关系运算符重载: 以 “==” 为例,其他符号类似
#include<iostream>
using namespace std;
class person {
public:
person(string name, int age) {
this->m_age = age;
this->m_name = name;
}
bool operator==(person p) {
if (this->m_age == p.m_age && this->m_name == p.m_name) {
return true;
}
else {
return false;
}
}
private:
string m_name;
int m_age;
};
void IsSame(person p1, person p2) {
if (p1 == p2) {
cout << "p1 == p2" << endl;
}
else {
cout << "p1!=p2" << endl;
}
}
void test() {
person p1("Lee", 20);
person p2("Lee", 20);
IsSame(p1, p2);
}
int main() {
test();
return 0;
}