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