What are the header defines for?

Discussion in 'C++' started by Marco Aschwanden, Dec 29, 2003.

  1. I am re-learning c++. And I see lots of code. All the time I stumble
    over the following pattern (in header files):

    #ifndef some_filename_h
    #define some_filename_h
    ..
    ..
    ..
    #endif

    What is this "filename" define good for? I found this pattern in many
    examples and in many books, but it is nowhere explained what it is
    good for? Can anyone explain this to me?

    Thanks a lot,
    Marco
     
    Marco Aschwanden, Dec 29, 2003
    #1
    1. Advertising

  2. Marco Aschwanden

    Sam Holden Guest

    On 29 Dec 2003 02:53:19 -0800,
    Marco Aschwanden <> wrote:
    > I am re-learning c++. And I see lots of code. All the time I stumble
    > over the following pattern (in header files):
    >
    > #ifndef some_filename_h
    > #define some_filename_h
    > .
    > .
    > .
    > #endif
    >
    > What is this "filename" define good for? I found this pattern in many
    > examples and in many books, but it is nowhere explained what it is
    > good for? Can anyone explain this to me?


    They are include guards and are used so that a header being included
    multiple times doesn't cause compilation errors (by defining something
    twice, for example).

    --
    Sam Holden
     
    Sam Holden, Dec 29, 2003
    #2
    1. Advertising

  3. Marco Aschwanden

    John Carson Guest

    "Marco Aschwanden" <> wrote in message
    news:
    > I am re-learning c++. And I see lots of code. All the time I stumble
    > over the following pattern (in header files):
    >
    > #ifndef some_filename_h
    > #define some_filename_h
    > .
    > .
    > .
    > #endif
    >
    > What is this "filename" define good for? I found this pattern in many
    > examples and in many books, but it is nowhere explained what it is
    > good for? Can anyone explain this to me?
    >
    > Thanks a lot,
    > Marco


    Suppose that you #include two files, "a.h" and "b.h" into the file
    "filename.cpp". Suppose that "b.h" itself #includes "a.h". That means that
    "a.h" is being #included into "filename.cpp" twice: once directly and once
    indirectly via "b.h". The scheme you have described stops this from
    happening.

    The first time "a.h" is #included, some_filename_h is not yet defined.
    Accordingly

    #ifndef some_filename_h

    returns true, some_filename_h is then defined, and the code that follows is
    read.

    The second time "a.h" is #included, some_filename_h has already been
    defined,

    #ifndef some_filename_h

    returns false and execution skips to the #endif, so none of the header code
    is read.


    --
    John Carson
    1. To reply to email address, remove donald
    2. Don't reply to email address (post here instead)
     
    John Carson, Dec 29, 2003
    #3
  4. EventHelix.com, Dec 30, 2003
    #4
    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. Replies:
    5
    Views:
    3,489
    Brian Drummond
    Feb 24, 2006
  2. Rob R. Ainscough
    Replies:
    1
    Views:
    421
    =?Utf-8?B?RGVhc3Vu?=
    Jun 27, 2005
  3. John Leonard
    Replies:
    2
    Views:
    387
    Rick Genter
    Sep 28, 2003
  4. theotyflos
    Replies:
    3
    Views:
    506
    Thomas Matthews
    Feb 19, 2004
  5. mlt
    Replies:
    2
    Views:
    906
    Jean-Marc Bourguet
    Jan 31, 2009
Loading...

Share This Page