1

我正在尝试转发声明一个派生自模板类的类,该模板类也必须被转发声明。

下面是一个类的例子:

class TType {
public:
    TType() { }
};

template<typename T>
class Base {
public:
    Base() { }
};

class Derived : public Base<TType> {
public:
    Derived() { }
};

这是我需要的失败猜测:

class TType;
template<typename T> class Base;
class Derived : public Base<TType>;  // This fails
Derived* pDerived;
4

1 回答 1

5

Just forward declare the class name:

class Derived;

You can't include any more information about a class in its declaration; base classes, members, etc. can only be declared in the class definition.

This forward declaration can be used to do various things, including declaring pointers or references (such as pDerived in your example), and also declaring functions with Derived as an argument or return type. If you need to do anything that needs to know the class's size, base classes, or members, then you'll need the full definition.

于 2012-04-05T16:23:06.580 回答