运算符重载示例
// coordinate.hpp
class coordinate
{
public:
int x;
int y;
coordinate();
coordinate(int x0, int y0);
//运算符重载对应的解析函数
coordinate operator+(coordinate& other);
};
// coordinate.cpp
coordinate coordinate::operator+ (const coordinate other)
{
//在该函数内,实现+的真正应该做的操作
coordinate tmp;
tmp.x = this->x + other.x;
tmp.y = this->y + other.y;
return tmp;
}
// main.cpp
int main(void)
{
coordinate a(1, 2);
coordinate b(3, 6);
coordinate c;
c = a + b; // 合法,会调用+符号的重载函数进行操作
return 0;
}


![【C++]运算符重载 【C++]运算符重载](http://www.mshxw.com/aiimages/31/352882.png)
