0

I have 3 classes, A, B, C.

A and B both have a protected member, let's call it x. It's a pointer.

C inherits from both A and B.

Question: Does C have its own copy of x now? If not, what does x mean now, B::x or A::x? How can C access its own x?

Btw, A and B are actually created using a template class so C looks like this:

class C : public A<someType>, public B<otherType> { 
   public:
      void metod() {
         this->x = 1 // doesn't work because now the member is ambiguous.
      }
}
user2297996
  • 1,382
  • 3
  • 18
  • 28
  • does this test compile atleast? – DipStax Dec 11 '20 at 09:11
  • disambiguate it `this->A::x` – Alberto Sinigaglia Dec 11 '20 at 09:15
  • No it doesn't compile. That's the point... – user2297996 Dec 11 '20 at 09:15
  • The meaning of `x` (unadorned) in `C` is ambiguous, since it can equally well refer to `A::x` or `B::x`. `C` does not have any member of its own called `x` unless you explicitly declare one. To use them, refer to `A::x` and `B::x` (or `this->A::x` and `this->B::x`) since they are distinct. This assumes that `A::x` and `B::x` are accessible (if `A` has a `private` member named `x`, then `A::x` is inaccessible to `C` - but its name still participates in name lookup). [Note - my description here is for the non-template `A` and `B` - adjust as appropriate for templated `A` or `B`]. – Peter Dec 11 '20 at 09:39

0 Answers0