Member Avatar for nalasimbha

In my project there is a need to for multiple inheritances and I have figured out that i need to inherit base classes virtually. The code that I have works fine, but I would like to know in particular what happens internally during inheritance.

The classes that i use and the way they are inherited are as follows:

class A
{
public:
double a;
};

class B:virtual public A
{
public:
double b;
};

class C:virtual public A;
{
public:
double c;
void c1();
};

class D: public B, public C
{
public:
double d;
};


class E:virtual public A
{
public:
double e;
void c1();
};

class F:public B, public E
{
public:
double f;
};

class G
{
public:
double g;
};

class H:public G, public D, public F
{
public:
double h;
};

For the declaration of class H do i need to use the keyword 'virtual' before classes D and F? What are the variables contained in class H after inheriting D and F and how many instances of the same variables would be in H?

Thank you in advance

Recommended Answers

All 6 Replies

This is a pretty rough hierarchy. There is still one ambiguity because you do not virtually inherit B in D and F, so there will be two instances of B in H. If you fix that, then H will have one copy of each class all the way up to A.

Member Avatar for nalasimbha

Thank you Tom Gunn. I would like to know if functions with same names in different classes be inherited all the time or will only one instance of the function be included in the derived class?

Would inheriting D and F virtually for class H solve the ambiguity?

I would like to know if functions with same names in different classes be inherited all the time or will only one instance of the function be included in the derived class?

Everything that can be inherited will be inherited, even if it causes an ambiguity.

Would inheriting D and F virtually for class H solve the ambiguity?

No, the ambiguity is in D and F, not H. Both D and F have an object of B, so the only way to fix that ambiguity is to virtually inherit B when deriving D and F.

Member Avatar for nalasimbha

Everything that can be inherited will be inherited, even if it causes an ambiguity.

But the purpose of using virtual inheritance is to avoid ambiguity while inheriting from multiple base classes

No, the ambiguity is in D and F, not H. Both D and F have an object of B, so the only way to fix that ambiguity is to virtually inherit B when deriving D and F.

Thanks.. i figured it out after i posted my reply :)

But the purpose of using virtual inheritance is to avoid ambiguity while inheriting from multiple base classes

Right. Virtual inheritance is kind of like inheriting a reference to a class instead of the class itself. All child classes use the reference instead of a separate copy and there is no ambiguity because there are no copies, just the original.

Member Avatar for nalasimbha

Thank you!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.