Everybody! This is important. In a few days, these forums will be moving over to using the totally sweet Discourse platform. To ensure this migration happens smoothly with no loss of content, these forums are currently in a read-only mode. I do apologize for the inconvenience.

There is never a good time to turn the forums off for an extended period of time, but I promise the new forums will be a billion times better. I'm pretty sure of it.

See you all on the other side in a few days, and if you have any (non-technical) questions, please e-mail me at kirupa@kirupa.com. For technical questions, try to find a tutorial that corresponds to what you are looking for and post in the comments section of that page.

Cheers,
Kirupa

Results 1 to 3 of 3

Thread: Organizing C/C++ code

  1. #1
    419
    posts
    Registered User

    Organizing C/C++ code

    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?

  2. #2
    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.
    If you notice this notice you will notice that this notice is not worth noticing.

    "Are you doing anything tonight? If not, how about me?"

    Opera Sucks! - FIX IT
    Oliver Zheng

  3. #3
    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:
      Code:
      #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

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012