Question in terms of design. Where to put "#include"?

Discussion in 'C++' started by Goran, Sep 3, 2007.

  1. Goran

    Goran Guest

    Hi all,

    this is just a "design" question.

    I'm unsure where to put my "#includes".

    Should I put it directly into headerfiles? Or into sourcefiles with a
    forward type declaration in headerfiles?

    And if forward type declarations in headerfiles are used... why can't
    I do something like...

    class string;

    and include string in the source file?

    Thanks

    Goran
    Goran, Sep 3, 2007
    #1
    1. Advertising

  2. Goran

    Bo Persson Guest

    Goran wrote:
    :: Hi all,
    ::
    :: this is just a "design" question.
    ::
    :: I'm unsure where to put my "#includes".
    ::
    :: Should I put it directly into headerfiles? Or into sourcefiles
    :: with a forward type declaration in headerfiles?

    It depends on how much of a class you really use in the header.

    To form a pointer or a reference, a declarations is enough. If you
    call a member function, for example, you need the full definition.

    ::
    :: And if forward type declarations in headerfiles are used... why
    :: can't I do something like...
    ::
    :: class string;
    ::
    :: and include string in the source file?

    Becase string isn't really a class, but a typedef for a template.


    Bo Persson
    Bo Persson, Sep 3, 2007
    #2
    1. Advertising

  3. On 2007-09-03 18:06, Goran wrote:
    > Hi all,
    >
    > this is just a "design" question.
    >
    > I'm unsure where to put my "#includes".
    >
    > Should I put it directly into headerfiles? Or into sourcefiles with a
    > forward type declaration in headerfiles?


    Using forward declarations if you can is a good idea.

    > And if forward type declarations in headerfiles are used... why can't
    > I do something like...
    >
    > class string;
    >
    > and include string in the source file?


    You can, sometimes. It depends on how you are using strings in your
    header, for some things the compiler need to know more about the type
    and then a forward declaration is not enough. As long as you only use
    references or pointers to string then you should be fine.

    --
    Erik Wikström
    =?ISO-8859-1?Q?Erik_Wikstr=F6m?=, Sep 3, 2007
    #3
  4. Goran

    BobR Guest

    Goran <> wrote in message...
    > Hi all,
    >
    > this is just a "design" question.
    > I'm unsure where to put my "#includes".
    >
    > Should I put it directly into headerfiles? Or into sourcefiles with a
    > forward type declaration in headerfiles?


    Put them in the .h and the .cpp. What if you later remove the header from
    the .cpp file. Something later in the chain might depend on the headers
    removed (in the .h).

    >
    > And if forward type declarations in headerfiles are used... why can't
    > I do something like...
    >
    > class string;
    >
    > and include string in the source file?
    > Thanks, Goran


    Put the headers everywhere they are needed. The inclusion guards prevent
    them being compiled a second time in a translation unit, and it's a form of
    documentation ( #include <string> == "Hey, I'm using std::string in this
    file!").


    A *weird* example I threw together to demonstrate how includes work:

    // --- Guts.h ---
    std::cout<<"Hello World, from Guts.h"<<std::endl;
    #define BLAHBLAH cout<<"Hello World, from Guts.h"<<std::endl;
    // be sure that is one single line!
    // --- Guts.h --- END

    // - Main.cpp -
    #include <iostream>
    int main(){
    #include "Guts.h" // yep, right here
    BLAHBLAH
    // note: no semicolon after that line.
    return 0;
    }
    // - Main.cpp -END

    The "Guts.h" totally depends on the "#include <iostream>" being exactly
    where it is in this case (not in the Guts.h file).
    This just shows that the compiler simply substitutes the contents of the .h
    file where it is included.
    You seldom, *if ever*, go to that extreme. <G>

    --
    Bob R
    POVrookie
    BobR, Sep 3, 2007
    #4
  5. Goran

    BobR Guest

    BobR wrote in message...
    >
    > // --- Guts.h ---
    > std::cout<<"Hello World, from Guts.h"<<std::endl;
    > #define BLAHBLAH cout<<"Hello World, from Guts.h"<<std::endl;


    Oops, that should be:
    #define BLAHBLAH std::cout<<"Hello World, from Guts.h"<<std::endl;
    BobR, Sep 3, 2007
    #5
  6. Goran

    Jorgen Grahn Guest

    On Mon, 03 Sep 2007 09:06:48 -0700, Goran <> wrote:
    > Hi all,
    >
    > this is just a "design" question.
    >
    > I'm unsure where to put my "#includes".
    >
    > Should I put it directly into headerfiles? Or into sourcefiles with a
    > forward type declaration in headerfiles?


    One common guideline (which some people disagree with) is that if you
    have a header file foo.h, a file which just contains

    #include "foo.h"

    should compile. I.e., foo.h should include everything it needs to
    compile (but not too much more).

    > And if forward type declarations in headerfiles are used... why can't
    > I do something like...
    >
    > class string;
    >
    > and include string in the source file?


    You /can/ do that, much of the time. There are rules for when you can
    and when you cannot. If you're talking about std::string, it won't
    work, because ... well, I guess std::string being a typedef is one
    reason?

    /Jorgen

    --
    // Jorgen Grahn <grahn@ Ph'nglui mglw'nafh Cthulhu
    \X/ snipabacken.dyndns.org> R'lyeh wgah'nagl fhtagn!
    Jorgen Grahn, Sep 3, 2007
    #6
  7. On 2007-09-03 23:47, Jorgen Grahn wrote:
    > On Mon, 03 Sep 2007 09:06:48 -0700, Goran <> wrote:
    >> Hi all,
    >>
    >> this is just a "design" question.
    >>
    >> I'm unsure where to put my "#includes".
    >>
    >> Should I put it directly into headerfiles? Or into sourcefiles with a
    >> forward type declaration in headerfiles?

    >
    > One common guideline (which some people disagree with) is that if you
    > have a header file foo.h, a file which just contains
    >
    > #include "foo.h"
    >
    > should compile. I.e., foo.h should include everything it needs to
    > compile (but not too much more).
    >
    >> And if forward type declarations in headerfiles are used... why can't
    >> I do something like...
    >>
    >> class string;
    >>
    >> and include string in the source file?

    >
    > You /can/ do that, much of the time. There are rules for when you can
    > and when you cannot. If you're talking about std::string, it won't
    > work, because ... well, I guess std::string being a typedef is one
    > reason?


    It should be possible, the problem is that it is a parametrised type, so
    you have to include all the template parameters in the forward
    declaration, which would require a forward declaration of char_traits
    too if I am not mistaken.

    --
    Erik Wikström
    =?ISO-8859-1?Q?Erik_Wikstr=F6m?=, Sep 3, 2007
    #7
  8. Goran

    Pete Becker Guest

    On 2007-09-03 18:24:12 -0400, Erik Wikström <> said:

    > On 2007-09-03 23:47, Jorgen Grahn wrote:
    >>
    >> You /can/ do that, much of the time. There are rules for when you can
    >> and when you cannot. If you're talking about std::string, it won't
    >> work, because ... well, I guess std::string being a typedef is one
    >> reason?

    >
    > It should be possible, the problem is that it is a parametrised type,
    > so you have to include all the template parameters in the forward
    > declaration, which would require a forward declaration of char_traits
    > too if I am not mistaken.


    Hypothetically, maybe. But the standard is explicit: if you use the
    name without including the standard header, the behavior is undefined.

    --
    Pete
    Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
    Standard C++ Library Extensions: a Tutorial and Reference
    (www.petebecker.com/tr1book)
    Pete Becker, Sep 3, 2007
    #8
  9. Goran

    James Kanze Guest

    On Sep 3, 7:05 pm, Erik Wikström <> wrote:
    > On 2007-09-03 18:06, Goran wrote:


    [...]
    > > And if forward type declarations in headerfiles are used... why can't
    > > I do something like...


    > > class string;


    > > and include string in the source file?


    > You can, sometimes. It depends on how you are using strings in your
    > header, for some things the compiler need to know more about the type
    > and then a forward declaration is not enough. As long as you only use
    > references or pointers to string then you should be fine.


    No. As others have pointed out, std::string is not a class, so
    the forward declaration doesn't work.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, Sep 5, 2007
    #9
  10. Goran

    Jim Langston Guest

    "Goran" <> wrote in message
    news:...
    > Hi all,
    >
    > this is just a "design" question.
    >
    > I'm unsure where to put my "#includes".
    >
    > Should I put it directly into headerfiles? Or into sourcefiles with a
    > forward type declaration in headerfiles?
    >
    > And if forward type declarations in headerfiles are used... why can't
    > I do something like...
    >
    > class string;
    >
    > and include string in the source file?


    Put the #include for any file that needs it, .h or .cpp. If you don't you
    get into the extremely irritating situation of order of includes. If an .h
    file needs to use the std::string class, #include <string> inside the
    header. If it was already included in some other header or the .cpp file no
    harm no foul. the include guards in <string> will make sure it's not
    included again. You can't do any harm by including an header file that's
    already been included somewhere else (with proper usage of include guards)
    but if you do not include a needed file it can be a real pain to maintain.
    Jim Langston, Sep 5, 2007
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Lori Cole via .NET 247

    Laman's terms please!

    Lori Cole via .NET 247, Aug 29, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    2,651
    bruce barker
    Oct 28, 2004
  2. Patrick Olurotimi Ige

    Asp vs ASPX pages in terms of security

    Patrick Olurotimi Ige, Feb 10, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    850
    Scott Allen
    Feb 10, 2005
  3. Andreas Bogenberger
    Replies:
    3
    Views:
    888
    Andreas Bogenberger
    Feb 22, 2008
  4. Gabriel Rossetti
    Replies:
    3
    Views:
    534
    Jerry Hill
    Apr 25, 2008
  5. Brock
    Replies:
    1
    Views:
    277
    Nathan Sokalski
    Oct 10, 2008
Loading...

Share This Page