Link to home
Start Free TrialLog in
Avatar of donzden
donzden

asked on

ambiguity errors,virtual and access specifiers....

suppose i have created two child classes from a parent class using public access specifier and then i create single child class out of these using multiple inheritance and now this will give me an ambiguity error.......to handle this we use keyword virtual....

but alternatively if i inherit the two child classes using private access specifiers the variables will not be inherited further so the ambiguity disappears so fundamentally the compiler should not complain....but still it gives me an error why so.......

i would be thankful if someone could answer this question for me......
Avatar of ntdragon
ntdragon

explane better what exectlly do you do

your problem is that when you make the two childs you don't make them virtual
i mean class child1:virtual class father
so when you make a son from the two childs the son gets thier father twice
and this is the error

about the private show exectlly what did you mean

It is not clear exactly what you are talking about, perhaps you could explain further.

But...the virtual inheritance is not to be used to resolve ambiguity (this is a side effect).  You use virtual inheritance to say that there should be only a single instance of the base class.

This may or may not be what you want.  Both cases are perfectly valid.

If you have ambiguous references in your code then you should remove the ambiguity using the scoping operator (::).  Like this:

class P
{ protected: int  mX; };
class C1 : public P {};
class C2 : public P {};
class MI : public C1, public C2
{ void f() { C1::mX = 4; C2::mX = 5; } };

Inside MI there are two instances of mX, one for C1 and one for C2.  

private inheritance would not change this.

Looks like an answer to me jason...
ASKER CERTIFIED SOLUTION
Avatar of Dhrubajyoti
Dhrubajyoti

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Dhrubajyoti, don't you think jasonclarke gave a better answer.
Frustrating isn't it.  

I didn't answer because I wasn't sure what I gave was what was wanted.
Avatar of donzden

ASKER

okay i would like to explain my question a bit more...

the ambiguity arises because the compiler doesn't know from where it should inherit which child because it finds the same variable in both the child classes.......which was inherited from the grand parent.

but when u make one of these child classes to be private, the variable is inherited into only one of the child classes and the ambiguity disappears....because now the compiler does not have two variables to inherit into the grand child.......

but still the compiler complains......why....that is my question
why did you accept that answer?

but, I suppose I will continue to help anyway...

private inheritance makes no difference to whether the variable is inherited or not.  When one class inherits from another, all of the objects in the base class are inherited, regardless of the type (public, protected or private) of the inheritance.

You can use virtual inheritance when you expect only one, common base class from a set of multiply inherited bases.  This may have the sode effect of reducing complexity.

if I am missing something, perhaps you could provide an example that demonstrates your point...

>> when u make one of these child classes to be private

Access specifyers do not affect the ambiguity, they are checked after the name resolution is performed [1]. And that name resolution results in an ambiguity.

struct Base{ int data; };
struct D1 : Base{};
struct D2 : Base{};
struct MI : D1, D2
{
     void f1()
     {
         data = 3; // which 'data' member, the one from D1 or D2
     }
     void f2()
     {
         D1::data = 3; // Now a full name is given, no ambiguity
     }

};

[1] I don't feel like ploughing through the standard right now, it's there somewhere.