There is this code:
#include <iostream>
class Base {
public:
Base(){
std::cout << "Constructor base" << std::endl;
}
~Base(){
std::cout << "Destructor base" << std::endl;
}
Base& operator=(const Base& a){
std::cout << "Assignment base" << std::endl;
}
};
class Derived : public Base{
public:
};
int main ( int argc, char **argv ) {
Derived p;
Derived p2;
p2 = p;
return 0;
}
The output after compilation by g++ 4.6:
Constructor base
Constructor base
Assignment base
Destructor base
Destructor base
Why assignment operator of base class is called altough it is said that assignment operator is not inherited?
You don’t have a default
in your
Derivedclass.A default assignment operator, however, is created:
and this calls the assignment operator from
Base. So it’s not a matter of inheriting the assignment operator, but calling it via the default generated operator in your derived class.