Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

ambiguity errors,virtual and access specifiers....

Posted on 2000-04-17
9
Medium Priority
?
318 Views
Last Modified: 2010-04-02
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
Comment
Question by:donzden
9 Comments
 
LVL 1

Expert Comment

by:ntdragon
ID: 2722059
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
 
LVL 9

Expert Comment

by:jasonclarke
ID: 2722376

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
 
LVL 7

Expert Comment

by:KangaRoo
ID: 2724258
Looks like an answer to me jason...
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Accepted Solution

by:
Dhrubajyoti earned 100 total points
ID: 2725474
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
 
LVL 7

Expert Comment

by:KangaRoo
ID: 2725929
Dhrubajyoti, don't you think jasonclarke gave a better answer.
0
 
LVL 9

Expert Comment

by:jasonclarke
ID: 2726003
Frustrating isn't it.  

I didn't answer because I wasn't sure what I gave was what was wanted.
0
 

Author Comment

by:donzden
ID: 2729140
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
 
LVL 9

Expert Comment

by:jasonclarke
ID: 2729832
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
 
LVL 7

Expert Comment

by:KangaRoo
ID: 2732209
>> 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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

885 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question