== Overload for Custom Class Is Not Always Called

Overloaded == operator in C++ is not called?

Task *t = new Task("Second task");
Task *t2 = new Task("Second task");
// ...
if( t == t2 ) {

You are not comparing Task objects, but pointers to Task objects. Pointer comparison is native to the language and compares identity of the objects (i.e. will yield true only if the two pointers refer to the same object or both are null).

If you want to compare the objects you need to dereference the pointers:

if( *t == *t2 ) {

How to handle operator == overload when the right hand side is of type Object

This makes me think that the test for reference equality is the expected behavior

This is correct.

It is very rarely useful to implement this in any other way, due to the fact that == is dispatched on the static type of the variables. If you implement ==(YourClass, Object) then this:

YourClass x = new YourClass();
if (x == someObject) { ... }

Will behave differently to:

Object x = new YourClass();
if (x == someObject) { ... }

This is usually unexpected!

So, if you want virtually-dispatched equality you should just use Equals.

There is only one type in System that implements an == with different argument types, and it's very special (RuntimeTypeHandle).

What's the right way to overload operator== for a class hierarchy?

For this sort of hierarchy I would definitely follow the Scott Meyer's Effective C++ advice and avoid having any concrete base classes. You appear to be doing this in any case.

I would implement operator== as a free functions, probably friends, only for the concrete leaf-node class types.

If the base class has to have data members, then I would provide a (probably protected) non-virtual helper function in the base class (isEqual, say) which the derived classes' operator== could use.


bool operator==(const B& lhs, const B& rhs)
return lhs.isEqual( rhs ) && lhs.bar == rhs.bar;

By avoiding having an operator== that works on abstract base classes and keeping compare functions protected, you don't ever get accidentally fallbacks in client code where only the base part of two differently typed objects are compared.

I'm not sure whether I'd implement a virtual compare function with a dynamic_cast, I would be reluctant to do this but if there was a proven need for it I would probably go with a pure virtual function in the base class (not operator==) which was then overriden in the concrete derived classes as something like this, using the operator== for the derived class.

bool B::pubIsEqual( const A& rhs ) const
const B* b = dynamic_cast< const B* >( &rhs );
return b != NULL && *this == *b;

overload operator within a class in c++

How do I overload the << operator for my_struct ONLY within the class?

Define it as

static std::ostream & operator<<( std::ostream & o, const my_struct & s ) { //...


namespace {
std::ostream & operator<<( std::ostream & o, const my_struct & s ) { //...

in the .cpp file in which you implement MyClass.

EDIT: If you really, really need to scope on the class and nothing else, then define it as a private static function in said class. It will only be in scope in that class and it's subclasses. It will hide all other custom operator<<'s defined for unrelated classes, though (again, only inside the class, and it's subclasses), unless they can be found with ADL, or are members of std::ostream already.

Related Topics

Leave a reply