Multiple Base Disambiguation

Sometimes, when more than one level of inheritance is in place, there can be ambiguity over which member belongs to which base class. Consider the case in which two classes DerivedClass1 and DerivedClass2 both derive from BaseClass:
class DerivedClass1: public BaseClass
class DerivedClass2: public BaseClass
and a class DerivedClass3 derives from DerivedClass1 and DerivedClass2:
class DerivedClass3: public DerivedClass1, public DerivedClass2
In class DerivedClass3 there are two different versions of BaseClass, one coming from DerivedClass1 and one from DerivedClass2. So if BaseClass contained a member function called setb(), an object of class DerivedClass3 calling such a member function:
DerivedClass3 anObject;
anObject.setb("hello");
would prevent the program from compiling, as the version of setb() to be used would not be known (would it be setb() within the BaseClass contained in DerivedClass1 or setb() contained in DerivedClass2?). To solve such a problem the scope operator must be used:
anObject.DerivedClass2::setb("hello");
In this way, the version of setb() to be used is clarified (in this case the one belonging to DerivedClass2).

Example


#include <iostream>
#include <cstring>
using namespace std;

class BaseClass
{
     string b;
public:
     void setb(string s)
     {
           b = s;
     }
};

class DerivedClass1: public BaseClass
{
     string d1;
public:
     void setd1(string s)
     {
           d1 = s;
     }
};

class DerivedClass2: public BaseClass 
{
     string d2;
public:
     void setd2(string s)
     {
           d2 = s;
     }
};

class DerivedClass3: public DerivedClass1, public DerivedClass2
{
     string d3;
public:
     void setd3(string s)
     {
           d3 = s;
     }
};

int main()
{
    DerivedClass3 anObject;
    // anObject.setb("hello"); this is an ambiguous statement
    // and would not compile
     anObject.DerivedClass2::setb("hello");
}