0% found this document useful (0 votes)
584 views2 pages

Ambiguity

C++ ambiguity occurs when a derived class has two base classes that share a common base class. This results in the derived class having two copies of the common base class. To access members of the common base class from the derived class, the scope resolution operator must be used to specify which base class path to use, as the compiler cannot differentiate between the two copies otherwise. The example demonstrates a class D derived from both B and C, where B and C share a common base of A, resulting in two copies of A in D. Accessing A through D requires specifying the B or C path.

Uploaded by

Ipsha Guha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
584 views2 pages

Ambiguity

C++ ambiguity occurs when a derived class has two base classes that share a common base class. This results in the derived class having two copies of the common base class. To access members of the common base class from the derived class, the scope resolution operator must be used to specify which base class path to use, as the compiler cannot differentiate between the two copies otherwise. The example demonstrates a class D derived from both B and C, where B and C share a common base of A, resulting in two copies of A in D. Accessing A through D requires specifying the B or C path.

Uploaded by

Ipsha Guha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

C++ Ambiguity

Ambiguity in C++ occur when a derived class have two base classes and these two base classes
have one common base class. Consider the followling figure:

Example of, occurrence of C++ ambiguity


#include<iostream.h>
#include<conio.h>

class ClassA
{
public:
int a;
};

class ClassB : public ClassA


{
public:
int b;
};
class ClassC : public ClassA
{
public:
int c;
};

class ClassD : public ClassB, public ClassC


{
public:
int d;
};

void main()
{

ClassD obj;

//obj.a = 10; //Statement 1, Error occur


//obj.a = 100; //Statement 2, Error occur

obj.ClassB::a = 10; //Statement 3


obj.ClassC::a = 100; //Statement 4
obj.b = 20;
obj.c = 30;
obj.d = 40;

cout<< "\n A from ClassB : "<< obj.ClassB::a;


cout<< "\n A from ClassC : "<< obj.ClassC::a;

cout<< "\n B : "<< obj.b;


cout<< "\n C : "<< obj.c;
cout<< "\n D : "<< obj.d;

Output :

A from ClassB : 10
A from ClassC : 100
B : 20
C : 30
D : 40

In the above example, both ClassB & ClassC inherit ClassA, they both have single copy of
ClassA. However ClassD inherit both ClassB & ClassC, therefore ClassD have two copies of
ClassA, one from ClassB and another from ClassC.
If we need to access the data member a of ClassA through the object of ClassD, we must specify
the path from which a will be accessed, whether it is from ClassB or ClassC, bco'z compiler can't
differentiate between two copies of ClassA in ClassD.

Avoid ambiguity using scope resolution operator


Using scope resolution operator we can manually specify the path from which data member a will
be accessed, as shown in statement 3 and 4, in the above example.
obj.ClassB::a = 10; //Statement 3
obj.ClassC::a = 100; //Statement 4

Note : still, there are two copies of ClassA in ClassD.

You might also like