1.全局函数重载
#include<iostream>
using namespace std;
#include<string>
class person{
public:
int m_a;
int m_b;
};
person operator+(person &p1,person &p2){
person temp;
temp.m_a=p1.m_a+p2.m_a;
temp.m_b=p1.m_a+p2.m_b;
return temp;
}
void test01(){
person p1;
p1.m_a=10;
p1.m_b=10;
person p2;
p2.m_a=20;
p2.m_b=20;
person p3=p1 + p2;
cout<<"p3.m_a:"<<p3.m_a<<endl;
cout<<"p3.m_b:"<<p3.m_b<<endl;
}
//全局函数重载
int main(){
test01();
return 0;
}
2.成员函数重载
person operator+(person &p){
person temp;
temp.m_a=this->m_a+p.m_a;
temp.m_b=this->m_a+p.m_b;
return temp;
}
3.函数重载
person operator+(person &p1,int num){
person temp;
temp.m_a=p1.m_a+num;
temp.m_b=p1.m_a+num;
return temp;
}