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?
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.
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.
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.
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.
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>.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With