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?
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?
You can use the
using
keyword to tell the compiler which version to use:This tells the compiler that the
Derived
class has a protected memberword
, which will be an alias toA::word
. Then whenever you use the unqualified identifierword
in theDerived
class, it will meanA::word
. If you want to useB::word
you have to fully qualify the scope.Your class
Derived
will have two variables,B::word
andA::word
Outside ofDerived
you can access them like this (if you change their access to public):Attempting to access
c.word
will lead to an error, since there is no field with the nameword
, but only A::word and B::word.Inside
Derived
they behave like regular fields, again, with the namesA::var
andB::var
also mentioned in other answers.It will be ambiguous, and you'll get a compilation error saying that.
You'll need to use the right scope to use it:
When accessing
word
in the class ofDerived
, you had to declare