Thread: Defining multiple classes in the same header file

  1. #1
    Registered User Stonehambey's Avatar
    Join Date
    Jan 2008
    Location
    Kent, UK
    Posts
    118

    Defining multiple classes in the same header file

    What are the rules of thumb for defining multiple classes in the same header file, with the added requirement that some of those classes are member variables for other classes?

    I was trying to create a linked list the other day, and my compiler kept telling me that it did not recognise some of the member variables within one of the node classes (which was a pointer to another class I had created)

    Regards

    EDIT: I hope this is ok to post here, as it's kinda an extension to the original Q. I didn't want to unnecessarily start a new thread
    Last edited by Stonehambey; 08-14-2008 at 09:43 AM.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Stonehambey View Post
    What are the rules of thumb for defining multiple classes in the same header file, with the added requirement that some of those classes are member variables for other classes?
    Classes that are ONLY used by another class should be declared INSIDE that class.
    For example:
    Code:
    class List
    {
       ... 
       class Node {
           Node *next;
           ... 
       };
    
       ... 
       Node *head;
    }
    Nothing outside of the linked list needs to know what the node looks like or what it holds. [Those class declarations are for illustration purposes only, and quite simplified]

    If they are independent classes that are used both in conjunction with each other and independently, then you have two choices:
    1. Declare multiple classes in one header file. Nothing directly wrong with it, but if you later on decide to use ONE of those classes in another project, the splitting is more difficult.
    2. Declare one class per header file, and include the header file for one object into the other as needed - or forward declare the class itself, and use only pointer/references to the class itself.

    Some people will have strong opinions about what you should or shouldn't do when it comes to header files and what you put in which. I take a fairly pragmatic view. Obviously, something is quite wrong if ALL your classes [in a large program] are declared in one header file and defined in one .cpp file. But putting a few closely related classes in one header file is fine. Think "Do these belong together." If not, use separate header files. "Can one of them be used without the other?" If not, put them in the same header file.

    Edit: I think this is a separate subject, so perhaps one of the moderators can split it...

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User Stonehambey's Avatar
    Join Date
    Jan 2008
    Location
    Kent, UK
    Posts
    118
    Quote Originally Posted by matsp View Post

    Edit: I think this is a separate subject, so perhaps one of the moderators can split it...
    Fair play, I wasn't sure. I'll start a new thread

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Class methods in header or source file?
    By TriKri in forum C++ Programming
    Replies: 13
    Last Post: 09-17-2007, 05:23 AM
  2. multiple file loading. so fruturated! help!
    By psychopath in forum Game Programming
    Replies: 5
    Last Post: 05-09-2005, 05:13 PM
  3. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  4. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM