English 中文(简体)
多功能建筑商是否多次被称作“多面”?
原标题:Are multiple-inherited constructors called multiple times?

多功能建筑商是否多次被称作“多面”? 所谓的建筑者是什么秩序? 这是否取决于继承名单上的顺序?

这里就是一个例子(它只是为了说明局势,没有真实生活的例子)。

class Base {};
class DerivedBaseOne : public Base {};
class DerivedBaseTwo : public Base {};
class Derived : public DerivedBaseTwo, public DerivedBaseOne 
{};

//somewhere in the code, is Base() called two times here?
Derived * foo = new Derived();

<代码>Base(> 构造者称为两倍吗? 而建筑商称之为什么顺序? 第一种基础? Or DerivedBase One( or DerivedBaseTwo( first?>?

最佳回答

http://www.ohchr.org。 DerivedBaseXXX Constructionor of which it is the subobject. 电话顺序按声明顺序排列。

相比之下,在您宣布的<代码>DerivedBaseXXX : 虚拟公共基础时,只有one 代码>Base subobject,其构造者从最衍生物体,即Derived的物体中标明。

(以更详细的方式解释:A类(可能是巧妙的)级是按第1条建造的,称为基类构造者,然后是2)称所有成员物体的构造者按其申报顺序排列,最后是3)执行施工机构。 这可追溯适用,而对于多重遗产,你只是以要求所有基类建筑商按宣布继承的顺序来取代(1)。 只有虚拟遗产使情况更加复杂。

问题回答

施工者要求继承等级的顺序是:

Base()  
DerivedBaseTwo()  
Base()
DerivedBaseOne()  
Derived()

该命令确实是明确的,取决于你提及基类衍生物的顺序以及你宣布成员为成员类别的命令。 (见下文C++标准。)

Does the Base() constructor get called twice?
YES

Base( 等构造者在此两次上课,因为两个班次DerivedBaseTwo(DerivedBase One(<>产生于此,因此基构造者每次一度就被召走。 页: 1

你拥有多重遗产的等级等级是罕见的,它导致了一个问题,即<>Diamond Shapedheritance Problem。 为避免这一问题,C++引进了


参考:

<>C++03 标准:12.6.2/5, 启动基地和成员

1. 初始程序应按下列顺序进行:

第一,只有下文所述最衍生类别的构造者,虚拟基级的起始顺序是按基级直系直系直观的左向直截盘顺序排列的,即“左对右”是指在衍生的基级基调清单中显示基类名称的顺序。

—— 然后,直接基类按申报顺序排列,按其出现在基点清单中(不论主要初始者的命令)。

—— 之后,非统计数据成员应按照在类别定义中宣布的顺序进行初步计算(不论异构体的次序)。

— Finally, the body of the constructor is executed.

http://www.paragia.com/c++-faq-lite/multiple-inheritance.html#faq-25.14“rel=“nofollow”http://www.paragia.com/c++-faq-lite/multiple-inheritance.html#faq-25.14

第一批被拆除的建筑是各等级的任何地方的虚拟基级。 他们按其出现在基级图表的深度一端左对右边的读物顺序执行,右边指基类名字的顺序。

由于您的多项继承声明清单如下:DerivedBaseTwo,其建筑命令将在之前执行。 DerivedBase One

因此,在您的<条码>衍生类别中,DerivedBaseTwo及其链条首先设定,即:

1-Base 然后DerivedBaseTwo

之后,DerivedBase One及其链条:

2-Base 然后,Derived Base One

之后:

3-Derived在一切其他情况下创建。

Also, with multiple inheritance be mindful of the Diamond Inheritance Problem





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签