English 中文(简体)
虚拟遗产传承者命令
原标题:Order of constructor call in virtual inheritance
class A {
        int i;
public: 
        A() {cout<<"in A s def const
";};
        A(int k) {cout<<"In A const
";  i = k; }
        };

class B : virtual public A {
public:
        B(){cout<<"in B s def const
";};
        B(int i) : A(i) {cout<<"in B const
";}
        };

class C :   public B {
public:
        C() {cout<<"in C def cstr
";}
        C(int i) : B(i) {cout<<"in C const
";}
        };

int main()
{
        C c(2);
        return 0;
}

The output in this case is

in A s def const
in B const
in C const

Why is this not entering into in A const

`It should follow the order of 1 arg constructor call. But what actually is happening on deriving B from A using virtual keyword.

还有几个问题:

即使我删除上述方案中的虚拟关键词,删除所有违约构造,也会造成错误。 因此,它为什么需要造物

问题回答

The constructors for virtual base classes are always called from the most derived class, using any arguments it might pass in. In your case, the most derived class doesn t specify an initializer for A, so the default constructor is used.

http://stackoverflow.com/a/10534300/241631” 因此,如果你想要<代码>A,则需要打字的构造者需要添加到<代码>上。 C 初始化清单。

C(int i) : A(i), B(i) {cout<<"in C const
";}

For the second part of your question, default constructors are not required, but then the derived class must call the non-default constructor explicitly, since the compiler is unable to do that for you in the absence of a non-default constructor.

#include <iostream>
using namespace std;

class A {
  int i;
public:
  // A() {cout<<"in A s def const
";};
  A(int k) {cout<<"In A const
";  i = k; }
};

class B : virtual public A {
public:
  // B(){cout<<"in B s def const
";};
  B(int i) : A(i) {cout<<"in B const
";}
};

class C :   public B {
public:
  C() : A(42), B(42) {cout<<"in C def cstr
";}
  C(int i) : A(i), B(i) {cout<<"in C const
";}
};

int main()
{
  C c(2), c2;
  return 0;
}

This prints out

In A const
in B const
in C const
In A const
in B const
in C def cstr

这里有两个问题。

为什么不卷入冲突?

因为你正在使用虚拟遗产。

http://www.paragia.com/c++-faq-lite/multiple-inheritance.html#faq-25.11”rel=“nofollow” 顶级主子的初始化名单直接援引虚拟基级主子。 在此情况下,这意味着<代码> C s Constructionor calls A s Constructionor directly。 由于您没有具体规定<代码>A,在<代码>上打字 C 初始化清单,即违约构造。

具体做法是将<代码>C:C(int)的实施改为:

C(int i) : A(i), B(i) {cout<<"in C const
";}

If I remove the virtual keyword in above program and remove all the default constructor it gives error. So, why it needs the def constructor?

编码。 此外,还注明了<代码>A的电梯,因此使用了违约构件。 请删除<代码>。 A s def ctor,B, t可加以汇编。





相关问题
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?