“+”运算符重载
#include <iostream>
using namespace std;
class coordinate
{
public:
int x;
int y;
public:
coordinate(int m_a,int m_b)
{
x = m_a;
y = m_b;
}
coordinate operator+(const coordinate &obj)
{
coordinate temp(0,0);
temp.x = this->x + obj.x;
temp.y = this->y + obj.y;
/* return coordinate(this->x + obj.x,this->y + obj.y); */
return temp;
}
};
/*
coordinate coordinate::operator+(const coordinate &obj);
{
coordinate temp;
temp.x = this->x + obj.x;
temp.y = this->y + obj.y;
return temp;
}
*/
int main()
{
coordinate coor1(3,5);
coordinate coor2(4,7);
coordinate coor3(0,0);
coor3 = coor1 + coor2;
cout <<coor3.x<<"\t"<<coor3.y<<endl;
return 0;
}
仿函数:
#include <iostream>
using namespace std;
class student
{
public:
string m_name;
void operator()(string talk)
{
cout <<m_name<<":"<<talk<< endl;
}
};
int main()
{
student t1;
student()("小 拜");
t1.m_name = "小 华";
t1("你好,你吃来了吗");
return 0;
}