Jump to content
  • Advertisement
Sign in to follow this  
schupf

C++ file organization/namespaces

This topic is 3613 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

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


Link to post
Share on other sites
Advertisement
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 this post


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


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


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


Link to post
Share on other sites
Quote:
Original post by SiCrane
I 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 this post


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


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


Link to post
Share on other sites
Quote:
Original post by schupf
Do 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 this post


Link to post
Share on other sites
Quote:
Original post by SiCrane
Quote:
Original post by schupf
Do 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)).

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!