Advertisement

VC++ Organising Code

Started by February 20, 2003 05:01 AM
3 comments, last by ardnut 21 years, 9 months ago
I have recently started learning c++ from several online tutorials and have got to the stage where I feel I can start writing a simple 2D game (Using OpenGL). At the momment I have just one .cpp file with all the code in it and this is starting to get a little to big to manage, so I want to split it up in to separate .cpp files. The problem is that I can''t find any tutorials on the web at all that deal with how to do this, what .h files need to be created and what goes in them, which scope to declear a variable so that it can be used from one .cpp file to another, etc... Any links to such a tutorial would be greatly appreciated.
The general rule that is commonly used is this:
Place the declaration of each public class in the ".h" file.
Place the implementation of each public class in the ".cpp" file.
A public class can contain zero or more private classes thus:
class A{private:    class B    {         // stuff    };    // more stuff}; 


So, every public class then has one ".h" and one ".cpp" and the filename is usually related to the class name: class vector3d would be declared in "vector3d.h" and implemented in "vector3d.cpp".

Skizz
Advertisement
I should have added that in the ".h" file you should either forward declare types used by the class (if they used as pointers) or #include the declaration, and in the ".cpp" you should #include declarations that are forward declared in the ".h" file.
A good habit to get into is in "vector3d.cpp" for example, put #include "vector3d.h" as the first include and this will catch any dependancy problems (if it works in the ".cpp" the ".h" can be included anywhere else without causing problems).
Also, the ".h" file should have a guard block:
#if !defined _VECTOR3D_#define _VECTOR3D_// all your declarations#endif // #if !defined _VECTOR3D_ 

This''ll work on any compiler.

Skizz
Many thanks for the quick response, I will try to implement your ideas into my program.
Just found this link to a GameDev article :D seems to cover the topic quite well.

http://www.gamedev.net/reference/programming/features/orgfiles/default.asp

This topic is closed to new replies.

Advertisement