Skip to main content
gamedev.net
🔒 Locked

Forward class declarations

Started by gommo Mar 20, 2003 at 11:09 PM 2 replies 250+ views
Original Post
gommo
gommo
How can you forward declare classes if you are inside a namespace I tried this (Lets say the namespace is Space) class Space::ClassName; #includes.... #includes.... namespace Space { class Space { blah.. }; } This doesnt seem to work.
Dactylos
Dactylos
This should work:
namespace Space
{
class ClassName;
}

#includes....
#includes....

namespace Space
{
class Space
{
blah..
};
}
gommo
gommo
Yeah, I did that it just looks ugly.
In the end I think I have got around the problem.

Am I right in believing you can forward declare classes more than once??
Xai
Xai
yes you can DECLARE a class, and almost anything else which isn''t a definition, an infinite amount of times ...

also ... be carefull with forward declaring inside a namespace, that you are actually doing what you mean to do ... ie - make sure that the item in question belongs IN the namespace ...

which also means ... do not include header files within a namespace, unless you mean to wrap it up into a namespace.

ALSO important ... if you have something like a static member (class member ... not C style static) which is in a class, in a namespace ... then it''s definition must be inside that namespace as well ... which made for some REALLY weird code with a template library I wrote once ...

I had a templated class, with a static member ... inside a namespace (lets all it A). Then I specialized this template inside other libraries over particular types, and those types we''re in a namespace (we''ll call it B).

So inside each .cpp file for each of these derived types, I had a situation where I had to do basically this:


  
#include "SomeClassHeader.h"

namespace A
{
template<>
... // specialization line, cannot remember offhand

}

namespace B
{
// actual class function implementation}


Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.