#include<iostream>
using namespace std;
#include<string>
class Person
{
public:
//bool operator==(const Person&p)//类内重载
//{
// return ((m_name == p.m_name) && (m_age == p.m_age));
//}
Person(string name, int age)
{
m_name = name;
m_age = age;
}
string m_name;
int m_age;
};
bool operator==(const Person& p1, const Person& p2)//类外重载
{
return ((p1.m_name == p2.m_name) && (p1.m_age == p2.m_age));
}
void test01()
{
Person p1("张三", 20);
Person p2("张三",198);
if (p1 == p2)
{
cout << "两人是同一人"<<endl;
}
else
{
cout << "两人不是同一人" << endl;
}
}
int main()
{
test01();
return 0;
}
等号运算符重载
最新推荐文章于 2024-11-07 15:40:17 发布