c++ (运算符重载 && 基本运算符重载)
c++ (运算符重载 && 基本运算符重载)
#define _CRT_SECURE_NO_WARNINGS
#includeusing namespace std;
class Person
{
public:
Person(){}
Person(int a,int b):m_A(a),m_B(b){}
int m_A;
int m_B;
//+号运算符重载
/*
Person operator+ (Person &p) {//二元
Person tmp;
tmp.m_A = this->m_A + p.m_A;
tmp.m_B = this->m_B + p.m_B;
return tmp;
}
*/
};
//利用全局函数
Person operator+ (Person &p1, Person &p2)//二元
{
Person tmp;
tmp.m_A = p1.m_A + p2.m_A;
tmp.m_B = p1.m_B + p2.m_B;
return tmp;
}
void text01()
{
Person p1(10, 10);
Person p2(10, 10);
Person p3 = p1 + p2;
cout << "p3的 m_A " << p3.m_A << " p3的m_B" << p3.m_B << endl;
}
int main()
{
text01();
return 0;
}