作用:重载关系运算符,可以让两个自定义类型对象进行对比操作


    示例:
    **

    1. class Person
    2. {
    3. public:
    4. Person(string name, int age)
    5. {
    6. this->m_Name = name;
    7. this->m_Age = age;
    8. };
    9. bool operator==(Person & p)
    10. {
    11. if (this->m_Name == p.m_Name && this->m_Age == p.m_Age)
    12. {
    13. return true;
    14. }
    15. else
    16. {
    17. return false;
    18. }
    19. }
    20. bool operator!=(Person & p)
    21. {
    22. if (this->m_Name == p.m_Name && this->m_Age == p.m_Age)
    23. {
    24. return false;
    25. }
    26. else
    27. {
    28. return true;
    29. }
    30. }
    31. string m_Name;
    32. int m_Age;
    33. };
    34. void test01()
    35. {
    36. //int a = 0;
    37. //int b = 0;
    38. Person a("孙悟空", 18);
    39. Person b("孙悟空", 18);
    40. if (a == b)
    41. {
    42. cout << "a和b相等" << endl;
    43. }
    44. else
    45. {
    46. cout << "a和b不相等" << endl;
    47. }
    48. if (a != b)
    49. {
    50. cout << "a和b不相等" << endl;
    51. }
    52. else
    53. {
    54. cout << "a和b相等" << endl;
    55. }
    56. }
    57. int main() {
    58. test01();
    59. system("pause");
    60. return 0;
    61. }