Jack Smother Jack Smother - 3 months ago 17
C++ Question

Multiple Inheritance: same variable name

class A
{
protected:
string word;
};

class B
{
protected:
string word;
};

class Derived: public A, public B
{

};


How would the accessibility of the variable
word
be affected in
Derived
? How would I resolve it?

Answer

It will be ambiguous, and you'll get a compilation error saying that.

You'll need to use the right scope to use it:

 class Derived: public A, public B
{
    Derived()
    {
        A::word = "A!";
        B::word = "B!!";
    }
};
Comments