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
}

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

Recommended Answers

All 4 Replies

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

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();

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.

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:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.