tags:

views:

211

answers:

3

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?

A: 

I don't really understand your question, but code like this:

template<class _T> class B;

is illegal. C++ reserves names begining with an underscore and an uppercase letter for the compiler & library implementation - you are not allowed to use them in your own code.

anon
+4  A: 

Yes you need. Note that all three lines do different things. The first declares a class. The second declares a template, and the third declares a typedef-name. In declaring the typedef-name, you can use an elaborated type specifier like "class A" to name the class without having its name in scope (with some nasty pitfalls included) - but you cannot further collapse the template declaration with the typedef declaration.

I would not worry about the three lines. If it's necessary, i would just write them out :)

Johannes Schaub - litb
I had hoped that, since I'm only using it for a private pointer, there would be a nice way to only reserve the necessary space without having to reveal the underlying details.
Jonas Byström
What do you mean? Can you please write code into the question how you wish it should look and how it looks now? I think we can then better understand it
Johannes Schaub - litb
+3  A: 

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>.

Ropez
EDIT: You don't need to forward declare "A" and "B" in the implementation file. Header files should be included.
Ropez
Not what I had in mind, but a great suggestion. Thanks!
Jonas Byström