C++ : 연산자 오버로딩(3) - 단항 연산자(증가, 감소 연산자의 오버로딩)
참고 : 열혈강의 C++ 프로그래밍 책
#include <iostream>
using namespace std;
class OperPoint2
{
private:
int x, y;
public:
OperPoint2(int px = 0, int py = 0) : x(px), y(py)
{ }
void ShowPosition() const
{
cout << '[' << x << ", " << y << ']' << endl;
}
OperPoint2& operator++()
{
x += 1;
y += 1;
return *this;
}
friend OperPoint2& operator--(OperPoint2 &ref);
};
OperPoint2& operator--(OperPoint2 &ref)
{
ref.x -= 1;
ref.y -= 1;
return ref;
}
// 테스트
void Operator2()
{
OperPoint2 pos(1, 2);
++pos;
pos.ShowPosition();
--pos;
pos.ShowPosition();
++(++pos);
pos.ShowPosition();
--(--pos);
pos.ShowPosition();
}
댓글
댓글 쓰기