[英]Why can't I access protected member in static polymorphic derived class?
This works fine: 这工作正常:
class A{
protected:
int i;
};
class B:public A{
public:
void f(){
i=5; //fine
}
};
However if I try to do the same with static polymorphism, it fails: 但是,如果我尝试对静态多态性做同样的事情,它将失败:
template <class Derived, typename T, int N>
class Vector{
protected:
std::vector<T> v;
Then: 然后:
template <typename T, int N>
class Vector234:public Vector<Vector234<T, N>,T,N>{
void test(){
T t=v[0]; // v is undeclared identifier
}
Why is this? 为什么是这样?
Because it's a dependent name, that is, it depends on the template parameter T
. 因为它是从属名称,所以它取决于模板参数
T
You need to explicitly specify either this->v[0]
or Vector<Vector234<T, N>,T,N>::v[0]
. 您需要显式指定
this->v[0]
或Vector<Vector234<T, N>,T,N>::v[0]
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.