Multiple inheritance without virtual functions in c++

左心房为你撑大大i 提交于 2019-12-04 17:56:48

问题


I came across the diamond problem and found different solutions for different cases with a single diamond. However I couldn't find a solution for 'chained' diamonds.

According to the structure: yes, I want to have multiple baseclasses everytime, so virtual inheritance isn't a solution (is it even called diamond then?). I also wanted to avoid get/set-functions for every middle-layer of a diamond.

p   p
|   |
k   k
 \ /
  s

class parent { int val; };
class kid1 : public parent {};
class kid2 : public parent {};
class school : public kid1, public kid2 {};

Accessing val in the parent class works now like follows:

school* s = new school;
s->kid1::val=1; // works

But what about the next 'chained' diamond:

p   p   p   p
|   |   |   |
k   k   k   k
 \ /     \ /
  s       s
  |       |
  c       c
    \   /
      w

class country1 : public school {};
class country2 : public school {};
class world : public country1, public country2 {};

Accessing val via:

world* w = new world;
w->country1::kid1::val=1; // error

results in:

error: ‘kid1’ is an ambiguous base of ‘world’

Why? Isn't the route to the value well defined?


回答1:


s->kid1::val does not mean "val from the kid1 subobject". It's just a name qualified by the type (not the subobject) that contains it.

I don't know why country1::kid1 is even accepted at all, but apparently it's a typedef for ::kid1. Two data members in world both have the qualified name ::kid1::val.

What you want is:

world* w = new world;
country1* const c1 = world;
c1->kid1::val = 1;



回答2:


It is. The error is due to a bug in your compiler.



来源:https://stackoverflow.com/questions/5353291/multiple-inheritance-without-virtual-functions-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!