Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 319
  • Last Modified:

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......
0
donzden
Asked:
donzden
1 Solution
 
ntdragonCommented:
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
0
 
jasonclarkeCommented:

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.

0
 
KangaRooCommented:
Looks like an answer to me jason...
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
DhrubajyotiCommented:
If you inherit the two child classes
using private access specifiers the public data member variable's will be inherited(you may have  public data member in c++)
   one more thing u may have function declared in the public section.
     Now for the inherited child class i have two base class function which have the same name smae return type and same argument as it derived from the first base class .

        so comiper should copmlain about this.      

0
 
KangaRooCommented:
Dhrubajyoti, don't you think jasonclarke gave a better answer.
0
 
jasonclarkeCommented:
Frustrating isn't it.  

I didn't answer because I wasn't sure what I gave was what was wanted.
0
 
donzdenAuthor Commented:
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
0
 
jasonclarkeCommented:
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...

0
 
KangaRooCommented:
>> 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.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now