基类和派生类的结构如下:
template<class T>
class Base {
public:
Base();
protected:
struct Foo{
int a;
int b;
};
int c;
};
template<class T>
class Derived : Base<T> {
public:
Derived();
...
};
我现在想在派生类中访问基类的受保护部分。对于c
或任何其他非类型成员,可以使用this->c
或Base<T>::c
。但是如何构造Foo的实例呢?this->Foo{x,y}
或Base<T>::Foo{x,y}
不工作,至少不与g++。
对于依赖类型,您需要使用typename
关键字来指示名称引用类型。
template <class T>
class Derived : Base<T> {
public:
Derived() : foo{1, 2} {}
typename Base<T>::Foo foo;
};