如何引用模板化基类的嵌套类型



基类和派生类的结构如下:

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->cBase<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;
};

最新更新