연산자 오버로딩의 이해
연산자 오버로딩의 이해
멤버함수에 의한 연산자 오버로딩
#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 add(const Point &ref) {
return Point(xPos + ref.xPos, yPos + ref.yPos);
}
};
int main() {
Point p1(10, 20);
Point p2(15, 25);
Point p3 = p1.add(p2);
p1.showPosition();
p2.showPosition();
p3.showPosition();
return 0;
}전역함수에 의한 연산자 오버로딩
이항 연산자의 다중정의
산술연산자의 다중정의
관계연산자의 다중정의
참고 사항
연산자 다중정의가 불가능한 연산자의 종류
멤버 함수 기반으로만 오버로딩 가능한 연산자 종류
연산자 오버로딩시 주의 사항
Last updated