954,496 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

hybrid inheritance prob

i m having one doubt regarding hybrid inheritance . i m just a beginner so its not clear to me :confused:
DOUBT:
WHY THE FOLLOWING CODE IS GIVING AMBIGUITY ERROR

#include<iostream.h>
class a
{
public:
 void f()
 {
 cout<<"base class\n";
 }
};
class b:private a //visibility mode private
{
public:
 void f1()
 {
 cout<<"inheritance\n";
 }
};
class c:public a  //visibility mode public
{
public:
 void f2()
 {
 cout<<"hybrid\n";
 }
};
class d:public b,public c
void main()
{
d w;
w.f(); //giving ambiguity error
}


<< moderator edit: added [code][/code] tags >>

f():private member of class b
f():public member of class c
as private members are not inheritable therefore there should be no
ambiguity error as class d can access f() through only one available
path that of class c
f()'saccess by w
w(object of class d)--->class c--->class a

please clear my doubt
i shall be highly thankful

gagan
Newbie Poster
3 posts since Jun 2005
Reputation Points: 10
Solved Threads: 0
 

<< moderator edit: merged threads >>

i m having one doubt regarding hybrid inheritance . i m just a beginner so its not clear to me :confused:
DOUBT:
WHY THE FOLLOWING CODE IS GIVING AMBIGUITY ERROR

#include<iostream.h>
class a
{
public:
 void f()
 {
 cout<<"base class\n";
 }
};
class b: private a //visibility mode private
{
public:
 void f1()
 {
 cout<<"inheritance\n";
 }
};
class c: public a  //visibility mode public
{
public:
 void f2()
 {
 cout<<"hybrid\n";
 }
};
class d: public b,public c
void main()
{
d w;
w.f(); //giving ambiguity error
}


<< moderator edit: added [code][/code] tags -- learn to do so yourself >>

f(): private member of class b
f(): public member of class c
as private members are not inheritable therefore there should be no
ambiguity error as class d can access f() through only one available
path that of class c
f()'saccess by w
w(object of class d)--->class c--->class a

please clear my doubt
i shall be highly thankful

gagan
Newbie Poster
3 posts since Jun 2005
Reputation Points: 10
Solved Threads: 0
 

Not my strong suit here, but isn't f() inherited by both b and c since it is public in a? And as such, there is ambiguity between which f() you are calling: b::f() or c::f()?

I found this would remove the ambiguity.

w.b::f();


Or.

w.c::f();
Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

Just a suggestion, try changing the function "void f()" to be "virtual void f()" and see if that resolves the issue. If it's just one of your classes acting up, you'll be able to tell if b or c is the troublemaker. If it doesn't work... well, someone competent will be with you shortly.

Drowzee
Posting Whiz in Training
245 posts since Jul 2005
Reputation Points: 22
Solved Threads: 5
 

thank u very much now my code is giving no ambiguity error but i have one
doubt regarding w.b()::f();
how class c object w can access f() through class b as f() is a private member of b :confused:

gagan
Newbie Poster
3 posts since Jun 2005
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You