ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

C虚拟继承初始化列表

2019-10-08 04:07:32  阅读:199  来源: 互联网

标签:virtual-inheritance c constructor inheritance multiple-inheritance


在以下代码中:

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

class B: public virtual A
{
public:
    B(int x):A(x){}
};
class C: public virtual A
{
public:
    C(int x):A(x){}
};
class D: public B, public C
{
public:
    D(int x):B(x++), C(x++), A(x++){}
};

两个问题:

>为什么我需要在D的初始化列表中添加A(…)?
> D(int x):B(x),C(x),A(x){}和D(int x):A(x),B(x),C(x){}都给出相同的结果与cout<< D(10).x,为什么?

解决方法:

Why do I need to add A(…) in D’s initializer list?

这是因为必须在所有其他子对象之前初始化虚拟基础子对象.由于A没有默认构造函数,因此需要在D中显式初始化虚拟A子对象,并指定要用它构造哪个参数.

当执行B和C基础子对象的构造函数时,它们将不具有要初始化的基础A子对象(已经在之前已经完成).因此,它们传递给A的构造函数的参数是无关紧要的.

D(int x):B(x++), C(x++), A(x++){} and D(int x):A(x++), B(x++), C(x++){} both give the same result with cout<<D(10).x, why?

如上所述,这是因为无论如何首先初始化虚拟基础子对象.

通常,类的子对象的初始化顺序决不会取决于它们在构造函数的初始化列表中出现的顺序.根据C 11标准第12.6.2 / 10段:

In a non-delegating constructor, initialization proceeds in the following order:

First, and only for the constructor of the most derived class (1.8), 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.

— 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).

— Then, non-static data members are initialized in the order they were declared in the class definition
(again regardless of the order of the mem-initializers).

— Finally, the compound-statement of the constructor body is executed.

标签:virtual-inheritance,c,constructor,inheritance,multiple-inheritance
来源: https://codeday.me/bug/20191008/1870392.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有