PDA

View Full Version : Organizing C/C++ code


Al6200
04-13-2007, 08:11 PM
How do you guys like to organize your C++ code? What I usually do is create a single .cpp file with my main loop, and then put all my support functions (graphics, keyboard, etc. ) in headers. But I've found that this often leads to confusion when two headers need to access one header, and I want to avoid headers being accessed twice.

How do you organize your C++ code? What methods have you found to be effective?

MTsoul
04-13-2007, 08:46 PM
You can use the old

#ifndef YOURHEADERNAME_H
#def YOURHEADERNAME_H

class declaration

#endif

for every header. That way nothing will be included more than once.

TheColonial
04-14-2007, 08:50 AM
Hi,

I would recommend not putting your entire class in the header file - that comes with a few issues if you're not careful and generally isn't good practice. You have to bear in mind that if you specify the body of a function in the class definition then by default that implentation is by default marked as 'inline' - this isn't something you want for all of your functions as it'll make your exes a lot bigger than they need to be. Also, bear in mind that when a header file is changed the source files that reference that header will be recompiled - so you should try and reduce the amount of includes, and the amount of code in the header files to reduce the time it takes for compilation. This is more of an issue for larger projects, but it's a principle that you should try to follow.

Here are some rules of thumb that are a good idea to follow:
Break each class up into two files a header (.h) and a source (.cpp). The names of the files should be the same as the class it contains. Eg. a class called MyClass should have its declaration/interface described in MyClass.h and it's definition/implementation described in MyClass.cpp.
Use the #ifndef/#define/#endif trick that MTsoul has described (but make sure you use #define instead of #def ;) ) to prevent multiple includes. If you use Visual C++ you could also use this trick at the start of your header files:#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000 which effectively does the same thing - but this is MS Visual C++ specific. Inline functions must be fully defined in the header file if those functions are to be used by other files. Including the function prototype in the header isn't enough. Template classes/functions fit into the same category as inline functions and should be totally defined inside the header file. Template classes generally don't have an accompanying .cpp file. If you're writing a library or an API, make sure that you put your header files in a different folder to the source - then you can ship your header files with your library with ease (usually you'd have an include folder and a source folder). Try and keep your header files as minimal as possible, and reduce the dependencies between them where you can. This helps keeps things tidy and reduces compile times. As your project size increases, break your source folder up into subfolders and group files together in logical folders (eg. move all graphics/rendering files to a folder called 'gfx' or something). This makes source management a lot easier and it doesn't take as much time to find the files you're looking for. Keep inline functions to a minimum to reduce code bloat - use them when really needed.

Hope that helps!
OJ