1

How do I use a function in class A from class D without inheriting class A and (although I understand it's bad practise, I am not permitted to alter the inheritance at all) and not remove the inheritance from C and B in D? I cannot seem to find a way around the request for member is ambiguous error. I was (wrongly) of the understanding that no matter how far removed the

class A 
{
public:
  void DoEverything(int){ }
};

class B : public A
{
public:
    ...
};

class C : public A
{
public:
    ...
};

class D : public C : public B
{
public:
    ...
};

int main() 
{
  D dMan;
  int i = 2;

  dMan.DoEverything(i);
}

My example is loosely based on the "Ambiguous base classes (C++ only)" example found here: http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/topic/com.ibm.xlcpp8a.doc/language/ref/cplr138.htm#cplr138 but that hasn't helped me understand the problem.

Hauleth
  • 22,873
  • 4
  • 61
  • 112
Switchkick
  • 2,716
  • 6
  • 21
  • 28
  • 1
    You are facing the diamond-inheritance problem. Check out this post: http://stackoverflow.com/questions/379053/diamond-inheritance-c – Marc Claesen May 18 '13 at 10:51

1 Answers1

5

First of all, to make it work just do the following changes

class B : virtual public A ...

class C : virtual public A

This problem in multiple inheritance is called the diamond problem. Check out this link to know more http://www.cprogramming.com/tutorial/virtual_inheritance.html

Saurabh Bhola
  • 2,940
  • 1
  • 13
  • 10