为什么派生类的构造函数要在C ++中初始化虚拟基类? [英] Why do the constructor of the derived classes want to initialize the virtual base class in C++?

查看:99
本文介绍了为什么派生类的构造函数要在C ++中初始化虚拟基类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的理解,例如阅读,是派生类的构造函数不会调用其虚拟基类的构造函数.

My understanding, for instance reading this, is that the constructor of a derived class does not call its virtual base class' constructor.

这是我做的一个简单示例:

Here is a simple example I made:

class A {
    protected:
        A(int foo) {}
};

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

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

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


int main()
{
    return 0;
}

出于某种原因,构造函数 B :: B() C :: C()试图初始化 A (,据我所知,此时应该已经由 D 初始化了):

For some reason, the constructors B::B() and C::C() are trying to initialize A (which, again in my understanding, should have already been initialized by D at this point):

$ g++ --version
g++ (GCC) 10.2.0
Copyright (C) 2020 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

$ g++ test.cpp
test.cpp: In constructor ‘B::B()’:
test.cpp:8:13: error: no matching function for call to ‘A::A()’
    8 |         B() {}
      |             ^
test.cpp:3:9: note: candidate: ‘A::A(int)’
    3 |         A(int foo) {}
      |         ^
test.cpp:3:9: note:   candidate expects 1 argument, 0 provided
test.cpp:1:7: note: candidate: ‘constexpr A::A(const A&)’
    1 | class A {
      |       ^
test.cpp:1:7: note:   candidate expects 1 argument, 0 provided
test.cpp:1:7: note: candidate: ‘constexpr A::A(A&&)’
test.cpp:1:7: note:   candidate expects 1 argument, 0 provided
test.cpp: In constructor ‘C::C()’:
test.cpp:13:13: error: no matching function for call to ‘A::A()’
   13 |         C() {}
      |             ^
test.cpp:3:9: note: candidate: ‘A::A(int)’
    3 |         A(int foo) {}
      |         ^
test.cpp:3:9: note:   candidate expects 1 argument, 0 provided
test.cpp:1:7: note: candidate: ‘constexpr A::A(const A&)’
    1 | class A {
      |       ^
test.cpp:1:7: note:   candidate expects 1 argument, 0 provided
test.cpp:1:7: note: candidate: ‘constexpr A::A(A&&)’
test.cpp:1:7: note:   candidate expects 1 argument, 0 provided

我敢肯定有些基本的东西我会被误解或做错了,但我不知道是什么.

I'm certain there is something very basic I misunderstood or am doing wrong, but I can't figure what.

推荐答案

已构建虚拟库的构造器.它是有条件构造的.即,最派生类的构造函数调用虚拟基数的构造函数.如果-这是条件-具有虚拟基础的派生类不是所构造对象的具体类,则它将不会构造虚拟基础,因为它已经由具体类构造了.但是否则它将构建虚拟基础.

The constructor of virtual base is constructed. It is constructed conditionally. That is, the constructor of the most derived class calls the constructor of the virtual base. If - this is the condition - the derived class with virtual base is not the concrete class of the constructed object, then it will not construct the virtual base because it has already been constructed by the concrete class. But otherwise it will construct the virtual base.

因此,您必须在所有派生类的构造函数中正确初始化虚拟基类.您只必须知道,如果具体类不是您正在编写的类,则不一定要进行特定的初始化.编译器不会也不知道您是否会创建这些中间类的直接实例,因此它不能简单地忽略它们损坏的构造函数.

So, you must correctly initialise the virtual base class in constructors of all derived classes. You simply must know that specific initialisation doesn't necessarily happen in case the concrete class is not the one which you are writing. The compiler doesn't and cannot know whether you will ever create direct instances of those intermediate classes, so it cannot simply ignore their broken constructors.

如果将那些中间类抽象化,那么编译器将知道它们永远不是最具体的类型,因此不需要初始化虚拟基的构造函数.

If you made those intermediate classes abstract, then the compiler would know that they are never the most concrete type and thus their constructor would not be required to initialise the virtual base.

这篇关于为什么派生类的构造函数要在C ++中初始化虚拟基类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆