Skip to main content
GameDev.net gamedev.net
🔒 Locked

Includes, defines and namespaces

Started by GameOxygen Jul 12, 2004 at 9:37 PM 7 replies 750+ views
Original Post
GameOxygen
GameOxygen
I have a simple question. I'm wondering if "#define" and "#include" should be inside or outside my namespace in the headers. I understand all "#include " should be outside namespace. like this:

#include "myotherheader.h"

#define PI 3.14
#define APP_VERSION 0.1

namespace myNamespace
{

// my classes and stuff
}

or like this:



namespace myNamespace
{
#include "myotherheader.h"

#define PI 3.14
#define APP_VERSION 0.1

// my classes and stuff
}

Miserable
Miserable
If you include a header inside a namespace, it will get "wrapped" in that namespace, so if myotherheader.h contains, say, function foo, it will now be myNamespace::foo. As for #defines, they do not obey any scope rules whatsoever and do not take namespaces into account (which is one reason why you should avoid defines and use const identifiers in situations such as the above.
GameOxygen
GameOxygen
Ok, so I should use const instead of defines and put them in the namespace then.

The other headers often have includes like "#include " so I guess i have to keep all the includes outside the namespace.
JohnBolton
JohnBolton
It generally doesn't make sense to put an #include inside a namespace because the declarations in the header file will be inside the namespace in some files, but not in others. That will cause problems.

Like Miserable wrote, #define is not affected by namespaces (or classes or functions or any other C++ constructs).
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
Xai
Xai
it is never safe to #include a general purpose header file into a namespace ... the only 2 times you #include into a namespace are to "wrap" the elements into the namespace, or if you have simply split a single file into multiple files for purely organizational reasons - and in this case it is NOT safe to #include the partial file in other places, so you should probably adopt the convention of using a different extension for these files, or just naming conventions such as: MyClass.h includes MyClass_PartA.h and MyClass_PartB.h, perhaps like: Vehicle.h includes Vehicle_PhysicalObject.h and Vehicle_Design.h or some such ... but the only file any other file should ever include would be Vehicle.h.
Infinisearch
Infinisearch
Sorry if this is highjacking the post but out of curiosity I was wondering

1.you can define a namespace in a header files right?
2.if you can and then you include one such header embedded in a namespace like the OP asked about, is that valid?
3.does that give you a hierarchial name space in the vein of
Thisfilesnamespace::theincludedheaderfilesnamespace::foo ???

Thanks in advance
-potential energy is easily made kinetic-
Miserable
Miserable
Quote:
Original post by Infinisearch
Sorry if this is highjacking the post but out of curiosity I was wondering

1.you can define a namespace in a header files right?
2.if you can and then you include one such header embedded in a namespace like the OP asked about, is that valid?
3.does that give you a hierarchial name space in the vein of
Thisfilesnamespace::theincludedheaderfilesnamespace::foo ???

Thanks in advance

I don't see why not; there's nothing wrong with nested namespaces.
Wavarian
Wavarian
Touching up on the reasons as to why it's not safe to place includes with a namespace:

anyHeader.h
class CAny{public:	void SomeMethod();};


anySource.cpp
void CAny::SomeMethod(){	// Some instructions}


main.cpp
namespace xxx{#include "anyHeader.h"}


In the above, the CAny definition is now part of the xxx namespace, HOWEVER, its implementation (in anySource.cpp) is not. Thus when you try to create an instance of CAny, and attempt to call SomeMethod() you will get an error - because the compiler won't see that the class method implementations were also supposed to be a part of the same namespace.

void CAny::SomeMethod() in anyHeader.h is now seen as xxx::CAny::SomeMethod(), and still CAny::SomeMethod() in anySource.cpp

Xai
Xai
Wavarian pretty much covered it perfectly ... please take heed of his post.

Topic Locked

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

Sign in to reply to this topic.