C++ includes

Discussion in 'C++' started by Alf P. Steinbach, Aug 15, 2005.

  1. * Jacob:
    > It is common recommendation that C++ header
    > files should be "self-sufficient", i.e. they
    > should directly include every concept it use
    > and not rely on getting this information
    > through other includes.
    >
    > Is this the case also for source files?


    A header file is a source file: with that interpretation the question is
    meaningless.

    If you mean module implementation files then the question assumes that a
    module implementation is #include'd in some other file, and then your
    question boils down to: should module implementation files be #include'd?

    And the answer to that is no.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Alf P. Steinbach, Aug 15, 2005
    #1
    1. Advertising

  2. Alf P. Steinbach

    Jacob Guest

    It is common recommendation that C++ header
    files should be "self-sufficient", i.e. they
    should directly include every concept it use
    and not rely on getting this information
    through other includes.

    Is this the case also for source files?

    Thanks!
    Jacob, Aug 15, 2005
    #2
    1. Advertising

  3. Alf P. Steinbach

    benben Guest

    A source file, of course, must contain all information necessary either by
    #including corresponding headers or having it as part of the file.
    Generally, if something in a source is not declared before use, you won't be
    able to compile it.

    Ben
    benben, Aug 15, 2005
    #3
  4. Alf P. Steinbach

    Jacob Guest

    benben wrote:

    > A source file, of course, must contain all information necessary either by
    > #including corresponding headers or having it as part of the file.
    > Generally, if something in a source is not declared before use, you won't be
    > able to compile it.


    An "include file" is declaration and protected againts
    multiple includes by a include guard, right?

    Consider the following two rudimentary include files:

    IncludeA.h:
    -----------
    #include <string>
    class A;


    IncludeB.h:
    -----------
    #include "IncludeA.h"
    class B {
    public:
    A a;
    string name;
    };


    I would claim it as an error for IncludeB.h *NOT* to include
    <string> as its declaration (B) depend on the string concept.

    You don't get a compiler error nor warning (perhaps a warning
    would be appropriate) by nut including it, as <string> is
    included indirectly through IncludeA.

    This is a common knowlegde and most styleguides will suggest
    you include <string> in this case to stay independent of internal
    changes in IncludeA.


    My question is if this should be the case also for difinition
    files (.c++ files)? You may claim that by including the declaration
    "IncludeA", you inherit the lot, and there is no need to include
    <string> separately as you got it for free already.

    Any suggestions?

    Thanks!
    Jacob, Aug 15, 2005
    #4
  5. Alf P. Steinbach

    benben Guest

    "Jacob" <> wrote in message
    news:...
    > benben wrote:
    >
    >> A source file, of course, must contain all information necessary either
    >> by #including corresponding headers or having it as part of the file.
    >> Generally, if something in a source is not declared before use, you won't
    >> be able to compile it.

    >
    > An "include file" is declaration and protected againts
    > multiple includes by a include guard, right?


    Correct!

    >
    > Consider the following two rudimentary include files:
    >
    > IncludeA.h:
    > -----------
    > #include <string>
    > class A;
    >
    >
    > IncludeB.h:
    > -----------
    > #include "IncludeA.h"
    > class B {
    > public:
    > A a;
    > string name;
    > };
    >
    >
    > I would claim it as an error for IncludeB.h *NOT* to include
    > <string> as its declaration (B) depend on the string concept.


    Correct!

    >
    > You don't get a compiler error nor warning (perhaps a warning
    > would be appropriate) by nut including it, as <string> is
    > included indirectly through IncludeA.


    No, you don't get a compiler error.

    >
    > This is a common knowlegde and most styleguides will suggest
    > you include <string> in this case to stay independent of internal
    > changes in IncludeA.


    That's correct!

    >
    >
    > My question is if this should be the case also for difinition
    > files (.c++ files)? You may claim that by including the declaration
    > "IncludeA", you inherit the lot, and there is no need to include
    > <string> separately as you got it for free already.


    Same rule, the source file #includes "IncludeA.h" for class A, "IncludeB.h"
    for class B, <string> for std::string.

    >
    > Any suggestions?
    >
    > Thanks!


    No worries!

    Ben
    benben, Aug 16, 2005
    #5
    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. Geraldine

    includes in ASP .NET

    Geraldine, Oct 28, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    301
    Kevin Spencer
    Oct 29, 2003
  2. Tor Nordahl

    WEBBOT includes and .ASPX

    Tor Nordahl, Nov 4, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    374
    Tor Nordahl
    Nov 4, 2003
  3. Steve

    Virtual Includes in ASP.NET

    Steve, Nov 19, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    407
    Jim Cheshire [MSFT]
    Nov 19, 2003
  4. Davíð Þórisson

    migrating asp to aspl.net: includes?

    Davíð Þórisson, Dec 30, 2003, in forum: ASP .Net
    Replies:
    4
    Views:
    372
    Davíð Þórisson
    Dec 30, 2003
  5. Jay

    Using includes with ASP.NET

    Jay, Jan 13, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    478
Loading...

Share This Page