【发布时间】:2013-07-08 09:00:17
【问题描述】:
我不明白复制顺序在类层次结构中是如何工作的
这段代码:
class Base
{
protected:
void myBaseMethod()
{
cout << "basemethod";
}
Base() { cout << "default constructor - base"; }
~Base() { }
Base(Base& other)
{
cout << "copy constructor - base";
}
Base& operator= (Base const &)
{
cout << "assignment operator - base";
}
};
class Derived : private Base
{
public:
Derived()
{
cout << "default constructor - derived";
}
};
int main()
{
Derived eaObj;
Derived efu = eaObj;
return 0;
}
按预期输出“默认构造函数 - 基”“默认构造函数 - 派生”,然后输出“复制构造函数 - 基”。
复制对象时会调用哪些复制构造函数?首先是基类,然后是派生类?如果它们是虚拟的呢?
【问题讨论】:
-
What if they're virtual?构造函数不能是虚拟的。 -
@Fiktik 他可能是指虚拟继承:如果基类是虚拟的会怎样。
标签: c++