Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Forward-declaring template pointer

Tags:

c++

templates

Do I really need three statements, i.e. like this

class A;
template<class _T> class B;
typedef B<A> C;

to forward-declare a pointer of template type C, like so:

C* c = 0;

I was hoping to be able to conceal the classes A and B in my forward-declaration, is that even possible?

like image 274
Jonas Byström Avatar asked Aug 23 '09 20:08

Jonas Byström


People also ask

Can you forward declare a template?

You can declare default arguments for a template only for the first declaration of the template. If you want allow users to forward declare a class template, you should provide a forwarding header. If you want to forward declare someone else's class template using defaults, you are out of luck! Save this answer.

What is forward declare in C?

In computer programming, a forward declaration is a declaration of an identifier (denoting an entity such as a type, a variable, a constant, or a function) for which the programmer has not yet given a complete definition.

Why Forward declare instead of include?

A forward declaration is much faster to parse than a whole header file that itself may include even more header files. Also, if you change something in the header file for class B, everything including that header will have to be recompiled.

What is function forward declaration?

A forward declaration allows us to tell the compiler about the existence of an identifier before actually defining the identifier. In the case of functions, this allows us to tell the compiler about the existence of a function before we define the function's body.


1 Answers

Although not exactly the same, you could do this instead:

class C;
C* c = 0;

and then later, in the implementation file, after the header files for "A" and "B" have been included, define "C" like this:

class C : public B<A> {};

Using inheritance instead of a typedef should work if you only need to use the default constructor of B<A>.

like image 116
Ropez Avatar answered Sep 22 '22 20:09

Ropez