<分区>
Possible Duplicate:
Why do I have to access template base class members through the this pointer?
我有一个如下所示的类层次结构:
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();
}
问题是 protected member variable t is not found in the Base class .编译器输出:
prog.cpp: In member function 'T Derived<T>::get()':
prog.cpp:10:22: error: 't' was not declared in this scope
这是正确的编译器行为还是编译器错误?如果它是正确的,为什么会这样?最佳解决方法是什么?
使用完全限定的名称是可行的,但似乎不必要地冗长:
T get() { return Base<T>::t; }