C++ check if two objects are equal with overloading == operator, always false?

后端 未结 2 1367
清歌不尽
清歌不尽 2021-01-26 03:03

I\'m attempting to check if two objects are equal by overloading the \'==\' operator for the class. Based on everything I\'ve read here on Stack Overflow and elsewhere, for exa

2条回答
  •  野的像风
    2021-01-26 03:54

    Your current operator== assumes the objects to be at the same address, so if you did something like

    PossibleChar p1;
    PossibleChar p2 = p1;
    std::cout << p1 == p2 << '\n';
    

    it would print false.

    If that is indeed what you want, alright.
    If it is not, you will need to define an operator==, which compares all necessary1 members of the objects, not the addresses.


    1 "necessary" means "members defining the state of the object" here, that is, it depends on the class which members should be compared.

提交回复
热议问题