template <class T>
class A
{
protected:
int _m;
public:
int getM() { return _m; }
void setM(int m) { _m = m; }
};
template <class T>
class B: public A<T>
{
public:
int getMM() { return this->_m; };
//int getMM2() { return _m; }; //error when use template, if no template, no problem at all.
class A
{
protected:
int _m;
public:
int getM() { return _m; }
void setM(int m) { _m = m; }
};
template <class T>
class B: public A<T>
{
public:
int getMM() { return this->_m; };
//int getMM2() { return _m; }; //error when use template, if no template, no problem at all.
};
If you omit this->
, the compiler does not know how to treat _m
, since it may or may not exist in all instantiations of A
. In order to tell it that _m
is indeed a member of A<T>
, for any T
, the this->
prefix is required. (even though any A<T> has the member, compiler didn't know)
OR
using A<T>::_m; // explicitly refer to a variable in the derived class