Possible Duplicate:
Why do I have to access template base class members through the this pointer?
I have a class hierarchy like the following:
template<typename T>
class Base {
protected:
T t;
};
template<typename T>
class Derived: public Base<T> {
public:
T get() { return t; }
};
int main() {
Derived<int> d;
d.get();
}
The problem is that the protected member variable t is not found in the Base class. Compiler output:
prog.cpp: In member function 'T Derived<T>::get()':
prog.cpp:10:22: error: 't' was not declared in this scope
Is that correct compiler behavior or just a compiler bug? If it is correct, why is it so? What is the best workaround?
Using fully qualified name works, but it seems to be unnecessarily verbose:
T get() { return Base<T>::t; }
To use members from template base classes, you have to prefix with this->
.
template<typename T>
class Derived: public Base<T> {
public:
T get() { return this->t; }
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With