class Base{
public:
virtual Base& operator=( const Base& ) {
std::cout << "Base::operator=( const Base& )" << std::endl;
return *this;
}
};
class Derived: public Base{
public:
virtual Derived& operator=( const Base& ) {
std::cout << "Derived::operator=( const Base& )" << std::endl;
return *this;
}
virtual Derived& operator=( const Derived& ) {
std::cout << "Derived::operator=( const Derived& )" << std::endl;
return *this;
}
};
int main(){
Derived obj1, obj2;
Base& ba=obj2;
obj1=ba;//outputs[1]: Derived::operator=( const Base&)
Derived& de=obj2;
obj1=de;//outputs[2]: Derived::operator=( const Derived& )
Base& bb=obj2;
bb=obj1; //outputs[3]: Derived::operator=( const Base&)
}
I am confused on the case 3 output, shouldn't assignment operator take the right operand as the function argument, so that obj1 is taken as the argument and since obj1 is class type of Derived, so it will call Derived::operator=( const Derived& )?
For the output[3], why the output is Derived::operator=( const Base& ), not Derived::operator=( const Derived& )?