简体   繁体   English

为什么我不能访问静态多态派生类中的受保护成员?

[英]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.

相关问题 为什么我不能从派生类的实例访问受保护的成员? - Why can't I access a protected member from an instance of a derived class? 无法从派生类访问基类中的受保护成员 - Can't access protected member in base class from derived class 无法访问派生类中受保护的类成员 - Can't access protected class member in a derived class 为什么派生类不能在此代码中调用受保护的成员函数? - Why can't a derived class call protected member function in this code? 为什么我不能使用受保护/私有继承在派生实例中访问基类的受保护成员? - Why can't I access protected members of base class in derived instances using protected/private inheritance? 为什么我不能访问派生构造函数的成员初始化列表中继承的受保护字段? - Why can't I access inherited protected fields in a derived constructor's member initialization list? 为什么派生类不能通过指向基类的指针访问其基类的受保护成员? - Why can a derived class not access a protected member of its base class through a pointer to base? 我们可以在派生类中访问基类的受保护成员函数吗? - Can we access protected member functions of base class in derived class? 基类可以访问 C++ 中的派生类受保护成员吗? - Can a base class access a derived class protected member in c++? 为什么我不能访问作为参数传递给函数的基类的受保护成员变量? - Why can't I access a protected member variable of a base class passed into a function as an argument?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM