Pregunta

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.

¿Fue útil?

Solución

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

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top