Sign in to follow this  
Mulligan

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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this