有这样一段代码:
#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;
}
使用 g++ 4.6 编译后的输出结果:
Constructor base
Constructor base
Assignment base
Destructor base
Destructor base
尽管说赋值运算符不被继承,但为什么基类的赋值运算符会被调用?