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();
}

댓글

이 블로그의 인기 게시물

C++ : Class로 만든 DLL Export 및 Import

Procmon 일부 기능

VMware 환경에서 우분투로 안드로이드 환경 구축하기(1)