단항 연산자 오버로딩
단항 연산자 오버로딩
전위 표기법
#include <iostream>
using namespace std;
class Point {
int xPos;
int yPos;
public:
Point(int x, int y)
: xPos(x), yPos(y) {}
void showPosition() const {
cout << "[" << xPos << ", " << yPos << "]" << endl;
}
Point& operator++() {
xPos += 1;
yPos += 1;
return *this;
}
friend Point& operator--(Point& ref);
};
Point& operator--(Point& ref) {
ref.xPos -= 1;
ref.yPos -= 1;
return ref;
}
int main() {
Point pos(10, 10);
++(++(++pos));
pos.showPosition();
--(--(--pos));
pos.showPosition();
return 0;
}후위 표기법
Last updated