# C++ file organization/namespaces

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

## Recommended Posts

Hello, I wonder if there is a common way to organize/name files in c++. Lets use this little example: I have a class TerrainRenderer and a class TerrainQuadTree, where the declarations are stored in headers TerrainRenderer.h and TerrainQuadTree.h. The definitions are stored in TerrainRenderer.cpp and TerrainQuadTree.cpp. Furthermore both classes are in the namespace Terrain. So if I want to use the TerrainRenderer class I have to write something like this: Terrain::TerrainRenderer* t = new Terrain::TerrainRenderer(...); While it is not wrong I am not really happy with this naming convention; its a little bit to verbose for my taste. Of course I could rename the class TerrainRenderer to Renderer to shorten it to: Terrain::Renderer. But then the file names wouldnt be correct, because all my files are named after the types they contain to keep everything simple. So I had to rename TerrainRenderer.(h/cpp) to Renderer.(h/cpp) but then I could run into conflicts if I add a namespace Sky which also has a renderer (Sky::Renderer), because this class Sky::Renderer would also need a file Renderer.h and Renderer.cpp. So how do you name your headers/source files if you use namespaces? Do you include the namespace name in the file name? Any suggestions how I could name my files/types?

##### Share on other sites
why not a solution with 2 projects one of them in your case would be Terrain or something like that where you place there the Renderer and stuff related, or simply place the terrain headers and files related in a folder named Terrain

##### Share on other sites
Splitting it up into 2 projects doesnt solve anything because the naming conflict could arise everytime (moreover I already have a sky and terrain so I definitely need both of them).
And creating folders (or filter's in visual studio) also doesnt help because they only exist virtually and have no influance on the type naming.

##### Share on other sites
Eventually you could put a using namespace terrain; in the implementation files since I don't see how duplicating the prefix helps.

##### Share on other sites
I generally put source files for a namespace into their own directory. So you'd have terrain/renderer.cpp and sky/renderer.cpp.

##### Share on other sites
Quote:
 Original post by SiCraneI generally put source files for a namespace into their own directory. So you'd have terrain/renderer.cpp and sky/renderer.cpp.

Hm, wait. In my visual studio 2005 solution I have a filter (dont know why but this is microsofts terminology for a folder) Terrain and this filter contains the file Renderer.h. But if I try to create a file Renderer.h in another filter (Sky) then visual studio refuses this action ("already exists in this project"). I understand this error because if I navigate to the project folder (where the .sln file and all the source files are saved) there are directories/filters - all .cpp and .h are just stored in the project folder, so its clear that I can't have two files Renderer.h. How do you manage to have 2 files with the same name?

##### Share on other sites
In MSVC 2005, filters don't actually correspond to folders on the hard drive. In MSVC, I create the files in the directories and then use add existing to put them in the right filters. Once added to your project, you might also want to change the output directory for the the object files. So if you have foo/baz.cpp, the default output would be $(IntDir). I would change it to$(IntDir)\foo\.

##### Share on other sites
Hm ok, this makes sense. I created a "src" directory in the project directory and for every namespace I create a folder, so I have /src/terrain and /src/sky. Do you know if I have to specify my new source input directory inside visual studio (and if yes: where)? I searched in the options but could not find anything like this and it looks like it works (does visual studio automatically search all directory inside the project directory for source files?)

Oh, another question:
So you have a file Renderer.h (which contains Terrain::Renderer) in the directory Terrain. If you use include guards for this header, how do you name the preprocessor constant? #ifndef TERRAIN_RENDERER (namespace + class name) or something like this?

##### Share on other sites
Quote:
 Original post by schupfDo you know if I have to specify my new source input directory inside visual studio (and if yes: where)?

I don't understand what you're asking here.

Quote:
 If you use include guards for this header, how do you name the preprocessor constant? #ifndef TERRAIN_RENDERER (namespace + class name) or something like this?

I use PROJECT_NAMESPACES_FILE.

##### Share on other sites
Quote:
Original post by SiCrane
Quote:
 Original post by schupfDo you know if I have to specify my new source input directory inside visual studio (and if yes: where)?

I don't understand what you're asking here.

Well, in visual studio you can set a lot of different directories/paths, for example you can specify where VS should search for include or library files, or you can define where VS should save the binary output. I just wonder if I also have to tell VS (per menu project->properties or somewhere else) where it should search for my sources (because now my sources are not just dumped in the project directory. Now they are stored in directories like /src/terrain (relative to the project directory)).

1. 1
2. 2
3. 3
4. 4
Rutin
13
5. 5

• 26
• 10
• 9
• 9
• 11
• ### Forum Statistics

• Total Topics
633696
• Total Posts
3013394
×