Assignment operator & inheritance

Started by
10 comments, last by Hodgman 10 years, 5 months ago

If the derived class "Node" did require an explicit overload, it would be for an assignment operator that takes an argument of the derived type, not the base type. Or perhaps I didn't fully understand your situation?

No you're right, there are probably not many cases where a derived type is assigned to a base type. I was simply being hypothetical.

As a side note, the standard way to do that is with pure virtual functions. If there's no other function that needs to be pure then make the destructor pure virtual. A class that people inherit from should generally have a virtual destructor anyway - you can run in to trouble when deleting an instance of the class if it's not.

I didn't know you could make the destructor pure virtual, thanks for the tip!

"I would try to find halo source code by bungie best fps engine ever created, u see why call of duty loses speed due to its detail." -- GettingNifty
Advertisement
Most of the time, I wouldn't thing that assignment would make sense with polymorphic types.

If B and C are implementations of the A interface, what does it mean to ask a B object to take on the values from a C object? They've got different internal representations, so can't be assigned...

I'd recommend looking into the noncopyable class (or other ways of disabling assignment) -- many polymorphic types are probably naturally non-copyable.
I actually make pretty much all of my classes non-copyable to begin with, until I actually need to be able to clone them ;)

This topic is closed to new replies.

Advertisement