class Base
{
public:
int i;
Base()
{
cout<<"Base Constructor"<<endl;
}
Base (Base& b)
{
cout<<"Base Copy Constructor"<<endl;
i = b.i;
}
~Base()
{
cout<<"Base Destructor"<<endl;
}
void val()
{
cout<<"i: "<< i<<endl;
}
};
class Derived: public Base
{
public:
int i;
Derived()
{
Base::i = 5;
cout<<"Derived Constructor"<<endl;
}
/*Derived (Derived& d)
{
cout<<"Derived copy Constructor"<<endl;
i = d.i;
}*/
~Derived()
{
cout<<"Derived Destructor"<<endl;
}
void val()
{
cout<<"i: "<< i<<endl;
Base::val();
}
};
如果我做 派生的d1; 導出的d2 = d1; 調用base的拷貝構造函數,並調用derived的默認拷貝構造函數。未調用的基本複製構造函數
但是,如果我從派生的複製構造函數中刪除註釋基本副本構造函數不會被調用。這有什麼特別的原因嗎? 在此先感謝。
IMVHO http://www.parashift.com/c++-faq-lite/ctors.html是理解C++構造函數的極好資源。 (實際上,C++ FAQ Lite通常是高級初學者的一個難以置信的信息源。) – notJim 2009-10-01 04:53:17
我認爲你的基礎析構函數應該是虛擬的。 – 2013-09-08 18:52:19