Ambiguity Resolution In CPP Programming

Outline 

In this article, we are going to know about the situation of Ambiguity, while implementing Inheritance and even learning how to resolve this using Virtual Base Class. Here we go.

Inheritance Overview

Before we proceed, we must have the prerequisite knowledge for inheritance. So Inheritance is one of the major pillars as per the OOPs concept in the CPP programming. Inheritance is a process by which objects of one class acquire the properties of objects of another class, and the CPP supports different paradigms of inheritance such as Single, Multiple, Multilevel, Hierarchical Inheritance, and so on.

The Introduction to Ambiguity

Basically, in general terms, Ambiguity refers to the condition that usually represents several interpretations from a single context. Coming to our technical stage, this will be followed in the concept of Inheritance, mainly we say in the context of Multipath Inheritance or Hybrid Inheritance. The hybrid inheritance is just a combination of more than one type of inheritance, say multilevel, multiple, or hierarchical. 

Let us understand the basic ideology of Hybrid Inheritance and the ambiguous concept with this illustration.

Now consider the concept shown in the figure above. There is a base class - A, and two derived classes - B and C have been derived from A. Also, another class D is derived from both classes B and C. This mainly signifies the concept of Hybrid Inheritance. And for the same, the program can be created. But, a compiler error may arise if a member function in the D class wants to access any member of the class twice because when B and C classes are derived from A, each inherits a copy of A's member. Now which one will be accessed by the function of D will be very confusing we say. Exactly, this situation is ambiguous and also known as the Diamond Problem.

Check out the following code for a logical understanding,

#include <iostream>
using namespace std;
class A {
    public: void display() {
    cout << "I Am The Base of ALL" << endl;
    }
};
class B: public A {};
class C: public A {};
class D: public B, public C {};
int main() {
    D obj;
    obj.display();
    return 0;
}

OUTPUT

Here comes the compiler error "request for the member display is ambiguous". As here for the object of class D will face the ambiguity in order to access the member of the function of A as through which class it access, whether to go with class B or class C. We can simply resolve this using the Scope Resolution operator '::' and even consider the "Virtual Base Class" functionality. Firstly covering with help of Scope ResolutionOperator.

Object_Name.Class_Name :: Function_Name(); // Syntax
#include <iostream>
using namespace std;
class A {
    public: void display() {
        cout << "I Am The Base of ALL" << endl;
    }
};
class B: public A {};
class C: public A {};
class D: public B, public C {};
int main() {
    D obj;
    obj.C::display(); // scope resolution operator used // obj.B::display(); can also be used
    return 0;
}

OUTPUT

Here you go..the output generated, as here the use of the :: operator will define the compiler to have the use of specified class functions that will resolve the paradox. Apart from this, now we will resolve this ambiguity using the next level feature that will redefine your working skills. Let's have a look at the basic introduction of this feature.

About Virtual Base Class

In simple words, the virtual base class is just a method used to forbid the different multiple objects of a class to be inherited during the application of multiple inheritances The duplication of inherited members due to these multiple paths can be avoided by making the common base as a virtual base class. The use of the keyword virtual will cause them to share a single common sub-object of their base class. The use of virtual bases class is not only limited to avoiding the ambiguity concept or to removing the hindrance of replicas, we say it even saves space as well and provides a way to have direct access to the classes.

// Syntax for the Virtual Base Class
class A // grandparent
{};
class B_one: virtual public A // parent 1
{};
class B_two: public virtual A // parent 2
{};
class C: public B_one, public B_two // child
{}; 
//only one copy of A will inherit..virtual and public may be used in either order

Consider the same code with the use of a virtual base class that resolves the situation of ambiguity that happened before.

#include <iostream>
using namespace std;
class A {
    public: void display() {
    cout << "I Am The Base of ALL" << endl;
    }
};
class B: virtual public A {};
class C: public virtual A {};
class D: public B, public C {};
int main() {
    A obj;
    obj1.display();
    D obj;
    obj.display();
    return 0;
}

OUTPUT

So what we have done here, is mainly make class A the virtual one so, with this, only a single copy of the data members and member function will be passed to class B and class C which helps to avoid duplication of data.

Summary

Lastly, in the end, we can state that the approach of the multilevel inheritance can result in the situation of the Ambiguity Error towards the compiler in the program and as in resolving manner the Virtual Base Classes are highly used that overcome the context of duplication by simply using the keyword virtual. Hope you clearly understand the motive of virtual base classes with respect to inheritance.


Similar Articles