为什么访问模板子类中的受保护成员需要父类前缀?

时间:2018-04-09 10:33:05

标签: c++ templates inheritance protected

template <class T>
class Parent {
protected:
  T _value;
};

template <class T>
class Child : public Parent<T> {
public:
  void doStuff() { std::cout << _value << std::endl; }
};

GCC 6.3给出了

prog.cpp: In member function ‘void Child<T>::doStuff()’:
prog.cpp:14:33: error: ‘_value’ was not declared in this scope
   void doStuff() { std::cout << _value << std::endl; }
                                 ^~~~~~

使用Parent<T>::_value代替_value有效。为什么呢?

我混淆的原因是使Child不是模板也可以。

class Child : public Parent<int> {
public:
  void doStuff() { std::cout << _value << std::endl; }
};
PS:在旁注中,它在任何情况下都与MSVC编好。我怀疑那是MSVC的错。

0 个答案:

没有答案