As I'm currently playing with C++, I come up to an issue. Code below:
#include <iostream>
class Base {
public:
virtual ~Base() {}
virtual Base& operator=(const Base& o)
{
std::cout << "Base operator called" << std::endl;
return *this;
}
};
class Derived : public Base {
public:
virtual ~Derived() {}
virtual Base& operator=(const Base& o)
{
std::cout << "Derived operator called" << std::endl;
return *this;
}
};
int main(void)
{
Derived a;
Derived b;
Base& c = a;
Base& d = b;
a = b; // Base called
a = static_cast<Base>(b); // Derived called
a = d; // Derived called
c = d; // Derived called
return (0);
}
The comment show what output I get. The last 3 results are very much predictable, but I can't understand the first.
As shown in the second one (static_cast), Derived::operator= is called when the right operand is a Base class. However, g++ (4.5.3-r2, gentoo Linux) success to understand that it must use the 'Base' class, but doesn't go down the inheritance tree.
So I was expecting either Derived::operator= to be called, or g++ complaining for no "Derived& Derived::operator=(const Derived&)". Could someone explain this behaviour to me ? Thanks!