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

Nested namespaces and classes

Started by stormwarestudios Oct 14, 2008 at 8:48 PM 0 replies 1.2k views
Original Post
stormwarestudios
stormwarestudios
Hi all, MSVC 2005 SP1 MS Windows XP SP2 I'm trying to learn a bit about using multiple levels of namespaces and classes in C++. I tried creating a sample app which looks like the following:

namespace N1
{
	namespace N2
	{
		class C1
		{
			class C2
			{
			};
		};

		class C3
		{
		};
	}

	namespace N3
	{
		class C4
		{
		};
	}
}




This compiles fine. However, when I try to split the appropriate classes into their own header files, I am having difficulty finding documentation on exactly how I should be doing this, syntactically speaking. I have header-guards in each header file, so this is not an issue. I'm just not sure how I should be defining, for example, C1/C2/C3/C4 in their own header files. Any help is appreciated. Cheers,
GenuineXP
GenuineXP
Nested classes will have to be declared in the same place as their parents (i.e., within the same header). The breakdown could look something like this, with three classes, one nested in another.

c1.hpp
...namespace n1 { namespace n2 {class c1 {public:    class c2 { ... };};}}...

c3.hpp
...namespace n1 { namespace n2 {class c3 { ... };}}...


Hope this helps. Note that the style is entirely up to you; I like to group namespaces on one line if nothing is contained in the intermediate area. I also don't tend to indent within namespaces. Unfortunately, C++ doesn't support a more brief syntax for namespaces such as C#'s.

EDIT: Whoops. Fixed missing brace. Also, remember that the implementation file (i.e., the *.cpp file) for c2 could be separate from that of c1's.

Topic Locked

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

Sign in to reply to this topic.