"Marco Aschwanden" <PP**********@spammotel.com> wrote in message
news:15*************************@posting.google.co m
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)