English 中文(简体)
钻石分问题:在分支部门中,非多重遗产仍需要同级建筑商。
原标题:Diamond sub-problem: non-multiple inheritance in side branch still require class constructor

当我试图以惯常的方式“解决”通常的钻石问题——利用虚拟遗产:

  A
 / * both virtual
B   C
  /
  D

然而,我的基类A don t有违约构造者,因此,我是用人工方式从D调来。 然而,当我试图在这批钻石中添加一个E类,作为C产地

  A
 / * both virtual
B   C
  / 
  D   E

仍然需要用人工方式将A型建筑商称为E型建筑商,即: 即便没有多重继承,也没有钻石A-C-E,但从E产生什么东西。

class A                     
   {public: 
      A (int _N): N(_N) {};
      void show()
        {cout<<"A"<<N;} 
    protected:
      int N;
   }; 
class B: public virtual A   
   { public: 
       B(int n): A(2*n) {};
       void show()
        { cout<<"B"<<N;} 
   }; 
class C: public virtual A   
   { public: 
       C(int n): A(3*n) {};
       void show()
        { cout<<"C"<<N;} 
   }; 
class D: public B,C 
   { public: 
       D(): B(1), C(2), A(3) {};
       void show()
        { cout<<"D"<<N;} 
   }; 

class E: public virtual C
   { public:
       E(): C(1) {};
       void show()
        { cout<<"E"<<N;} 
   }; 

int main()
  {D d;       // OK
   A *a = &d; 
   a->show(); 

   E e;        // NOT OK, no function A::A() to call in E::E()
   A *a2 = &e;
   a2->show();
   return 0;
  } 

如果不从E呼吁A建造者,能否解决这一问题? 我需要C做得适当......。

或者不可能试图完全解决钻石问题:

A   A
|   |  no virtual at all
B   C
  / 
  D   E

并且仍在试图宣布D类的标语,其中两例是A,但告诉汇编者在每次从D进行核对时使用A类C? 当我试图补充时

using C::A

在宣布D时,它仍然产生明确的基础A的错误。

最佳回答

如果不从E呼吁A建造者,能否解决这一问题? 我需要C做得适当......。

最衍生产品类别(此处为E)的施工人负责召集任何虚拟基地类别的施工人。

<代码>C的构造者不能称呼<代码>A,因为<代码>C不是最衍生的类别。 虚拟基级为:在<><>>>之前>任何直接基级,即<代码> E必须先通过<代码>A启动,然后才能开始<编码>C。

问题回答

是的,虚拟基级建筑群的电话与虚拟功能不符:

  • you can override a virtual function in a derived class;
  • you must override a virtual function only if the function is overridden in one of the base classes but not in others;
  • you cannot not override the init-list for virtual base classes of the base class constructor.

这意味着:

  • as far as virtual function overriding is concerned, virtual inheritance does not affect single inheritance at all;
  • but when it comes to base class constructor calls, virtual inheritance affects every derived class.




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