有這樣的代碼:賦值運算符繼承
#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
爲什麼基類的賦值運算符稱爲altough據說賦值運算符不遺傳?
[C++中運算符的繼承問題]可能的重複(http://stackoverflow.com/questions/3882186/trouble-with-inheritance-of-operator-in-c) – 2012-02-06 14:29:49