namespace question

Recommended Posts

Mulligan    378
I'm going through tons of code right now, adding namespaces to stop name clashing that is popping up, but dont know if I should include headers within the namespace. Example: file.h
namespace Game
{
#include <windows.h>

class foo
{
...
};
}


OR

#include <windows.h>

namespace Game
{
class foo
{
...
};
}


Does it matter, or is one more correct?

Share on other sites
soconne    105
I don't think it should matter if all your namespaces are in different header files. But I would just put the includes at the top of your file, outside the namespace.

I use the Irrlicht engine and it makes use of namespaces heavily, and I don't think any header files are included inside the namespaces themselves.

Share on other sites
petewood    819
If you put windows.h in the Game namespace then all the functions in there will be in the namespace. It will have the effect of modifying the declarations of the functions as they'll now be things like Game::GetDeviceCaps instead of ::GetDeviceCaps. The libraries you are linking against will have ::GetDeviceCaps in them.

On the whole it's not good practice to wrap other peoples headers in a namespace, although there may be (dubious) reasons why you would want to do it with your own files.

Pete

Share on other sites
Mulligan    378
makes sense. Thanks evrahbodah

Create an account

Register a new account

• 9
• 11
• 9
• 10
• 14