Sol*_*olx 1 c++ templates class derived forward
我试图转发声明一个派生自模板类的类,该类也必须是前向声明的.
以下是类的示例:
class TType {
public:
TType() { }
};
template<typename T>
class Base {
public:
Base() { }
};
class Derived : public Base<TType> {
public:
Derived() { }
};
Run Code Online (Sandbox Code Playgroud)
这是对我需要的失败猜测:
class TType;
template<typename T> class Base;
class Derived : public Base<TType>; // This fails
Derived* pDerived;
Run Code Online (Sandbox Code Playgroud)
只需转发声明类名:
class Derived;
Run Code Online (Sandbox Code Playgroud)
您不能在其声明中包含有关类的任何其他信息; 基类,成员等只能在类定义中声明.
此前向声明可用于执行各种操作,包括声明指针或引用(例如pDerived在您的示例中),以及Derived使用参数或返回类型声明函数.如果您需要做任何需要知道类的大小,基类或成员的事情,那么您将需要完整的定义.