为什么在这种情况下删除了 grand parent 的构造函数?

Why is constructor of a grand parent deleted in this case?

编译器抱怨 D 的构造函数被删除,因为格式不正确,为什么?

#include<iostream>
using namespace std;

class A
{
   int x;
   public:
   A(int i) { x = i; }
   void print() { cout << x; }
};

class B: virtual public A
{
   public:
      B():A(10) { }
};

class C: virtual public A 
{
   public:
      C():A(10) { }
};

class D: public B, public C {
};

int main()
{
   D d;
   d.print();
   return 0;
}

输出

main.cpp:37:4: error: use of deleted function 'D::D()' D d; ^ main.cpp:32:7: note: 'D::D()' is implicitly deleted because the default definition would be ill-formed: class D: public B, public C { ^

那是因为 D 使用 virtual 间接继承自 A。 A 没有无参数构造函数,因此无法生成 D 的编译器生成的构造函数。

由于虚拟基的初始化规则类,

class D: public B, public C {
};

相当于:

class D: public B, public C {
    public:
       D() : A(), B(), C() {}
};

这就是为什么您不能在 D 实例中创建的原因。

解决方案 1

更改 A 使其具有默认构造函数。

class A
{
     int x;
   public:
     A(int i = 0) { x = i; }
     void print() { cout << x; }
};

解决方案 2

D更改为:

class D: public B, public C {
    public:
       D() : A(0), B(), C() {}
};

或更简单的版本,

class D: public B, public C {
    public:
       D() : A(0) {}
};

注意:这主要只是添加了对标准的引用,以防万一有人关心(但对他来说,@R.Sahu 的回答一如既往地准确)。

标准规定 ([class.base.init]/13):

In a non-delegating constructor, initialization proceeds in the following order:
(13.1) — First, and only for the constructor of the most derived class (6.6.2), virtual base classes are initialized in the order they appear on a depth-first left-to-right traversal of the directed acyclic graph of base classes, where “left-to-right” is the order of appearance of the base classes in the derived class base-specifier-list.
(13.2) — Then, direct base classes are initialized in declaration order as they appear in the base-specifier-list (regardless of the order of the mem-initializers).

所以,由于A是一个虚拟基class,它直接由最派生的class(D)初始化。只有在这之后,直接基 classes 才被初始化——但是对于任何要编译的东西,最派生的 class 必须能够初始化虚拟基 class(es)。

在这种情况下,有些人可能会觉得有一点很有趣。让我们稍微修改一下 class 结构,以便我们进行必要的初始化,并且(重要的)在每个构造函数中使用唯一值进行初始化:

#include <iostream>

class A {
    int i;
 public:
    A(int i) : i(i) {}

    void show() { std::cout << "value: " << i << "\n"; }
};

class B : virtual public A{
public:
    B() : A(10) {}
};

class C : virtual public A { 
public:
    C() : A(20) {}
};

class D : public B, public C {
public:
    D() : A(0) {}
};

int main() {
    D d;
    d.show();
}

在这种情况下,究竟发生了什么?我们有三个不同的构造函数,每个构造函数 "thinking" 它会用不同的值初始化 A 对象?哪一个 "wins"?

答案是最派生的构造函数 (D::D) 中的构造函数是用于初始化虚拟基础 class 对象的构造函数,因此它就是 "wins".当我们 运行 上面的代码时,它应该打印 0.