Solx
Solx

Reputation: 5111

How do I forward declare a class derived from a forward declared template base class?

I am trying to forward declare a class that is derived from a template class that must also be forward declared.

Here is an example of the classes:

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

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

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

Here is a failed guess at what I need:

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

Upvotes: 1

Views: 2064

Answers (1)

Mike Seymour
Mike Seymour

Reputation: 254431

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.

Upvotes: 5

Related Questions