继承树中的奇怪复制构造函数行为

时间:2013-09-14 02:21:02

标签: c++

我有三种类型:Base-> Center-> Child,我想让它可以从父类型构造子类型,所以我声明它们是这样的:

class Base {
public:
    Base(void) {cout << "Base()" << endl;}
    virtual ~Base(void) {}
    Base(const Base &ref) {cout << "Base(const Base &)" << endl;}
};
class Center : public virtual Base {
public:
    Center(void) : Base() {cout << "Center()" << endl;}
    Center(const Base &ref) : Base(ref) {cout << "Center(const Base &)" << endl;}
};
class Child : public virtual Center {
public:
    Child(void) : Center() {cout << "Child()" << endl;}
    Child(const Base &ref) : Center(ref) {cout << "Child(const Base &)" << endl;}
};

可以像这样调用它:(调用Center和Base的复制构造函数)

Base base;
Center center(base);


但是,这些代码意外地表现出来:

Child child(base);

输出是:

Base()
Center(const Base &)
Child(const Base &)

为什么调用Base(void)而不是Base(const Base&amp;)?

已解决(非常感谢DietmarKühl和Adam Burry) 两种方法:

1. add Base(const Base &) to Child, which would looks like this:
   Child(const Base &ref) : Base(ref), Center(ref) {}
2. or, remove virtual inheritance, which would looks like this:
   class Center : public Base {...}
   class Child : public Center {...}

1 个答案:

答案 0 :(得分:2)

virtual基类的参数由派生程度最高的类提供,而不是由中间类提供。如果您想将Child收到的参数传递给Base,则需要像这样实现Child构造函数:

Child::Child(Base const& other)
    : Base(other)
    , Center(other)
{
}

顺便说一句,使用(void)而不是()来表示该函数在C ++中不接受任何参数是不常见的:void在某些时候需要在C到区分变量参数列表函数和不参数的函数(我不知道C11中是否仍然如此)。在C ++中,一对空括号总是表示一个空参数列表。

相关问题