Jump to content
  • Advertisement
Sign in to follow this  
MrRage

C++ Namespacing

This topic is 5458 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I was wondering if compilers (GCC or MSVC) would let you use multiple namespaces in a '.h' file. I think I tryed this before and it didn't work. Example: #include <gdk.h> using namespace gdk::gfx; using namespace gdk::engine; namespace gdk{ namespace engine{ class Object { public: Surface* surface; // gdk::gfx::Surface Particle particles; // gdk::engine::Particle ... }; } }

Share this post


Link to post
Share on other sites
Advertisement
not sure, but I don't see why not... after the precompile, its compiles like it's all in one file anyway...

Share this post


Link to post
Share on other sites
true.. I'm sure this works with MSVC (no compile errors) but I would have to try it out on a GCC compiler to see if it will accept it. MSVC will let me do things GCC won't.

Share this post


Link to post
Share on other sites
I think MSVC is using some old standards, that's probably why, but the namespace thing should work on GCC.

Share this post


Link to post
Share on other sites
Absolutely. That is valid C++ and there is no reason why any compiler worth its salt should complain about it.

At compilation time, there is really no difference between .h and .cpp files. All those include statements in a .cpp file simply cause the header file code to get pasted into the file when compiling, so it's all the same thing to the compiler. The only reason header files even exist is so that you don't have to manually redeclare everything in each .cpp file you write.

By the way, I've used namespaces like your example in both Visual C++ .NET and 2003 without any problems. It should probably work with Visual Studio 6 as well.

Share this post


Link to post
Share on other sites
another question, can you simply do...
namespace gdk::engine
{

}


vs.


namespace gdk{
namespace engine{
}
}

Share this post


Link to post
Share on other sites
I don't believe that last bit will work (using namespace gdk::engine). I'd say that engine and gdk have seperate namespace scopes at some point in time, and that, as such, the compiler won't assume that they're always connected.

What I'm trying to say is, even if you design one to always come after the other, the compiler treats them as seperate namespaces. Then again, if you declare a namespace within a namespace, then I guess the new namespace is only a part of the original namespace.

So, in summary, I don't know, you should test it yourself.

Share this post


Link to post
Share on other sites
Quote:
Original post by MrRage
another question, can you simply do...
namespace gdk::engine
{

}


vs.


namespace gdk{
namespace engine{
}
}


No, that does not work in C++. That's not part of the specifications, and all compilers sticking to the C++ standard wont alow namspaces like that.

If you were programming in C# on the other hand, then it works just fine.

Share this post


Link to post
Share on other sites
could you alias the namespace?
not sure if this would work (dont have a compiler handy)


namespace gdkEngineAlias = gdk::engine;

namespace gdkEngineAlias {
// my code...
};

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!