Thread: C++ project structure

  1. #1
    sockets mad
    Join Date
    Mar 2002

    C++ project structure

    I've read quite a few books and other stuff about C++ but they don't seem to go into a huge amount of detail about how to structure an increasingly large project into different files so it's more easily navigatable. There's a few questions that I'd like anyone who can help me out to answer:

    1) My cpp files seem to have quite a large amount of code before the first function, e.g include files from the Platform SDK etc, odd one or two global variables and some other stuff. What's the best practice to cut this info down? The way I'm doing it at the moment is to make a header file with the same name as the cpp and put some of it in there leaving only lines like #include <windows.h> in the top of the cpp file and including it like #include "myprog.h". Am I going about it the right way?

    2) When I start moving functions that are related into seperate files to make it easier to navigate my app I have to start adding function prototypes at the top of the calling cpp file which calls the functions in the newly created file so I don't generate undeclared identifier errors. This seems to make more clutter at the top of the file and sometimes extern variable declarations to access things like CRITICAL_SECTION variables also clutter up the file. what's the best way to organise all this stuff?

    I think you can probably guess my general problem, those were only a couple of examples.

    I can post some examples if it helps at all.



  2. #2
    Registered User
    Join Date
    Feb 2003
    What I've been told, the general rule of thumb would be to seperate each class to their own .cpp and header would have the class defintions (functions and variables) in the header file, and then write all the code in the cpp file, and include the header file....

  3. #3
    sockets mad
    Join Date
    Mar 2002
    I knew about how to seperate class code up between header and cpp file but it's the normal structured programming stuff I'm a little confused about but thanks anyway.


  4. #4
    Registered User
    Join Date
    Mar 2002
    The way I think about it is that everything you take out of the current program that allows it to work without adding lots of #includes, etc. you will have to put into a file that you link to sooner or later. That's why looking into a header like windows.h is such a night mare. It's merely window dressing for a bunch of other files, that contain a bunch of other files they have linked to, etc, etc. True, once you get everything set up you only write windows.h in your program, but there is a lot of code written behind that simple little phrase. Likewise, all of your code will need to be out there somewhere. In my experience, when trying to get functions/variables shared between files, etc., the hassles you are experiencing can be minimized with experience, but not eliminated.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 13
    Last Post: 12-14-2007, 03:34 PM
  2. can not create Visual C++ Smart Device project
    By George2 in forum Tech Board
    Replies: 0
    Last Post: 11-26-2006, 06:20 AM
  3. passing structure arrays to functions?
    By bem82 in forum C Programming
    Replies: 3
    Last Post: 10-30-2006, 06:17 AM
  4. 2D RPG Online Game Project. 30% Complete. To be released and marketed.
    By drallstars in forum Projects and Job Recruitment
    Replies: 2
    Last Post: 10-28-2006, 12:48 AM
  5. Dynamic Binding
    By gpr1me in forum C++ Programming
    Replies: 1
    Last Post: 03-24-2006, 09:01 AM