개념부터 설명하자면 연산자 오버로딩은 사용자 클래스 타입을 연산 할 수 있게 하는 문법이다.
#include <iostream>
using namespace std;
class Point
{
int x;
int y;
public:
Point(int _x = 0, int _y = 0) : x(_x), y(_y) {}
void Print()const { cout << x << ',' << y << endl; }
void operator+(Point arg)
{
cout << "operator+() 함수 호출" << endl;
Point pt;
pt.x = this->x + arg.x;
pt.y = this->y + arg.y;
return pt;
}
};
int main()
{
Point p1(2, 3), p2(5, 5);
p1 + p2;
return 0;
}
Point 클래스 내에 operator+ 연산을 정의해두었고 main에서 Point 객체 p1,p2의 +연산하면 p1.operator+(p2)와 같이 연산 된다.