关系运算符:大于、小于、等于、不等。
关系运算符重载:可以让两个自定义类型对象进行对比操作。
#include<iostream>
using namespace std;
class Person
{
public:
Person(string name, int age)
{
m_Name = name;
m_Age = age;
}
bool operator==(Person p)
{
bool temp = false;
if (this->m_Age == p.m_Age && this->m_Name == p.m_Name)
{
temp = true;
}
return temp;
}
string m_Name;
int m_Age;
};
void test01()
{
Person p1("Tom", 18);
Person p2("Tom", 18);
if (p1 == p2)
{
cout << "p1和p2相等。" << endl;
}
else
{
cout << "p1和p2不相等。" << endl;
}
}
int main()
{
test01();
system("pause");
return 0;
}