1

I have a templated class that I inherit from, and access through the derived class the base class members. I can't access them without using "this" and I find a descent reason why.

If I understand correctly, when I use a template, a copy of the templated code is being made with a specialization, and only then it compiles. Meaning if I write vector<int> the compiler makes a copy of vector and replaces all the "T" with "int".

If that is the case, I don't see why there should be any difference between templates and non-templated code.

template <typename T>
class b
{
protected:
 int myMember;
};

template<typename T>
class e : public b<T>
{
public:
 void dosomething()
 {
  this->myMember = 2; // Everything is perfect
  myMember = 2; // Doesn't compile in GCC, can't find the member
 }
};

int main()
{
 e<int> mye;
 mye.dosomething();
}
OopsUser
  • 4,642
  • 7
  • 46
  • 71

1 Answers1

2

Because the base class is dependent on template parameters, the base members are not considered during unqualified lookup.

When you use this-> you use class member lookup instead, which will examine the base class members, even if it is a dependent type.

TartanLlama
  • 63,752
  • 13
  • 157
  • 193