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