Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple inheritance without virtual functions in c++

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?

like image 255
nem Avatar asked Mar 18 '11 14:03

nem


1 Answers

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;
like image 61
Ben Voigt Avatar answered Oct 10 '22 16:10

Ben Voigt