[C++] 단항 연산자 오버로딩

KWANHO PARK·2025년 4월 18일

CPP

목록 보기
14/24
/*
	단항 연산자 오버로딩: 반환형 operator[연산자]()
*/
#include <iostream>

class Money {
private:
	int money;
public:
	Money(int m = 0) : money(m) {
	}
	int operator()() {
		return money;
	}
	void operator()(int m) {
		money += m;
	}
	
};

int main()
{
	Money m;
	printf("money: %d\n", m.operator()());
	m(1000);
	printf("money: %d\n", m.operator()());
	m.operator()(1000);
	printf("money: %d\n", m.operator()());
	return 0;
}

0개의 댓글