Jump to content
  • Advertisement
  • 11/04/00 12:15 AM
    Sign in to follow this  

    Putting it all together: How to best arrange C++ source and header files

    General and Gameplay Programming

    Myopic Rhino
    When I first began to learn C++ about a year ago, I was used to the good old-fashioned one-source-file model. It took me weeks to figure out how C++ arranged and compiled its source and header files. It took me over a month to figure out how to use this arrangement to create globals instantly. I am writing this to save the other beginners out there the heartache.

    Here is my basic model that I use in all my applications:

    ------------------------------------------------------------------------------
    Globals.h is the only header file included in any source file - here is how
    it's set up inside:
    ------------------------------------------------------------------------------
    #ifndef MadeIncludes //if we haven't done this where this file can see it, then...
    #define SCREENX = 640
    #define SCREENY = 480
    //and other app-wide #defines

    #include
    #include
    //and other system or lib includes

    #include "creature.h" //class header or some function prototypes

    enum Enumeration{E_THINGY1, E_THINGY2};
    #define MadeIncludes
    #endif

    extern int globalint;
    extern char globalstring[50];
    //and other globals

    ------------------------------------------------------------------------------
    Winmain.cpp (or other main() or winmain() module)
    ------------------------------------------------------------------------------
    #include "globals.h"
    int globalint;
    char globalstring[50];
    //and other globals

    //and then the rest of the main file's source code

    ------------------------------------------------------------------------------
    Creature.cpp is my implementation for Creature.h that's included in globals.h
    ------------------------------------------------------------------------------
    #include "globals.h" //you see, this now has access to all globals and header files

    class creature
    {
    int x;
    int y;
    //whatever
    };

    //and other classes/structs

    void MoveCreature(int newx, int newy);
    //and other various procedures
    ------------------------------------------------------------------------------


    I hope this helped you guys. Questions, comments, whatever, e-mail me at [email="benbeandogdilts@cs.com"]benbeandogdilts@cs.com[/email]. Visit my web page at www.geocities.com/benbeandogdilts. Have fun, guys!


      Report Article
    Sign in to follow this  


    User Feedback


    There are no comments to display.



    Create an account or sign in to comment

    You need to be a member in order to leave a comment

    Create an account

    Sign up for a new account in our community. It's easy!

    Register a new account

    Sign in

    Already have an account? Sign in here.

    Sign In Now

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!