Solved

ambiguity errors,virtual and access specifiers....

Posted on 2000-04-17
9
311 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
 
LVL 1

Accepted Solution

by:
Dhrubajyoti earned 50 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

705 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now