Thread: Class Inheritance over multiple source files

  1. #1
    Registered User Swarvy's Avatar
    Join Date
    Apr 2008
    Location
    United Kingdom
    Posts
    195

    Class Inheritance over multiple source files

    I need to get several classes to inherit from one another, but for the sake of organisation in the project I'm working on at the moment, I want to have them in different source code files.

    At the moment, I am doing it like this:
    Code:
    /* Contents of foo.h */
    
    class Foo
    {
               private:
    
               public:
                        void func(char *cstring);
    
    };
    
    
    /* Contents of bar.h */
    
    #include "foo.h"
    
    class Bar : public Foo
    {
              private:
    
              public:
                     /* Methods */
    
    };
    But, efficiency is a big part of the program I am writing, I want to get maximum efficiency, and so I was wondering, if I do it like this, won't the file foo.h in the example get compiled over and over again? since I have about 8 or 9 other header files getting compiled off one header file which contains this "super class", lol.

    I was wondering if there was a better way of doing it to get more efficient code. also, is my program stucture the best one to use?

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I would be included over and over, yes... The only way to avoid that is to use a forward-declaration where you can.
    The you need to include those files so that the compiler can see the classes it is deriving from.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If you've got anything in foo.h that actually gets compiled, then it's broken (multiple symbol definitions or whatever). #include just reads the file and dumps it in.

  4. #4
    Registered User Swarvy's Avatar
    Join Date
    Apr 2008
    Location
    United Kingdom
    Posts
    195
    Quote Originally Posted by Elysia View Post
    I would be included over and over, yes... The only way to avoid that is to use a forward-declaration where you can.
    You think that I should include the class declaration in the main cpp file before the other headers are included?

    Thanks for being so quick btw, lol.

    edit: Likewise tabstop

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Did not say that. It is bad practice to have headers relying on other headers without including them.
    What I meant is that if your other headers refer to the your super-header, but does not actually use it, you can use a forward-declaration.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Including it in every source file won't have any effect on the speed of the program, only on the speed of compilation...

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Also, if you have correct include guards in your header files, including the same file multiple times will not affect the compile time (much [1]) as there will only ever be one copy of the actual contents of the header file.

    [1] I believe most compilers (technically, the preprocessor part of the compiler) actually DETECT the fact that a certain header file has a include guard, and if it's been included already, don't include it again. That way, there's extremely little overhead of having the same header file(s) included multiple times.

    --
    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.

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    That doesn't apply to multiple compilation units, though.

    And the most prominent of those that don't detect this is MSVC++.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. added start menu crashes game
    By avgprogamerjoe in forum Game Programming
    Replies: 6
    Last Post: 08-29-2007, 01:30 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 05:01 PM
  4. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  5. Replies: 4
    Last Post: 06-18-2005, 02:26 PM