Possible Duplicate:
Trouble with inheritance of operator= in C++
hello everyone, let’s assume I have two classes:
Base{}; //inside I have operator=
Derived{}; //inside I don't have operator=
why this one is working perfectly:
Derived der1, der2;
der1=der2; //<-here I know that it actually calls my Base::operator=
and this one is not:
Derived der1;
Base bas1;
der1=bas1; //<-here why can't call Base::operator=?
The implicitly declared copy assignment operator looks like
This implicitly declared function calls
operator=for each base class and member subobject (this is why theBaseclassoperator=overload is called).bas1is of typeBase, notDerived, and there is no implicit conversion fromBasetoDerived, hence it doesn’t work. You would need to declare an appropriate assignment operator in order to support assigning an object of typeBaseto an object of typeDerived(this would be a bit unusual though).