Thanks for the replies chaps, but gah, I've given the example wrong. Dammit, sorry. Let me try again.
class MyBaseClass
{
virtual MyBaseClass& operator = (const MyBaseClass& that);
};
class MyDerivedClass : public MyBaseClass
{
virtual MyDerivedClass& operator = (const MyBaseClass& that);
};
And the following code:
MyDerivedClass MyObj1;
MyDerivedClass MyObj2;
MyObj1 = MyObj2;
I would have expected this to work the same as the example that mbue posted above - but in my code, it doesn't.
I would expect that MyObj2 would be implicitly cast to a MyBaseClass and the overridden assignment operator in the derived class called. But it isn't; the base class version is called. I have to add:
MyDerivedClass& operator = (const MyDerivedClass& that) { return operator = (static_cast<const MyBaseClass&>(that)); }
to explicitly accept an argument of my derived class.
Again, is there something I'm missing?