有这样的代码:#include
有这样的代码:
#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
为什么虽然说赋值运算符不能被继承,但却调用基类的赋值运算符呢?