The following code fails to compile (ideone):
#include <iostream>
template <typename T>
class A {
public:
A(T* t) : t_(t) {}
protected:
T* t_;
};
template <typename T>
class B : public A<T> {
public:
B(T* t) : A<T>(t) {}
T get() { return *t_; }
};
int main()
{
int i = 4;
B<int> b(&i);
std::cout << b.get() << std::endl;
}
The error is as follows:
prog.cpp:17:20: error: use of undeclared identifier 't_'
T get() { return *t_; }
^
By my understanding, B<T>
should have access to protected members of its parent class. Why does B<T>
not see the protected member A<T>::t_
?