 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Marco Aschwanden Guest
|
Posted: Mon Dec 29, 2003 10:53 am Post subject: What are the header defines for? |
|
|
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
|
|
| Back to top |
|
 |
Sam Holden Guest
|
Posted: Mon Dec 29, 2003 11:00 am Post subject: Re: What are the header defines for? |
|
|
On 29 Dec 2003 02:53:19 -0800,
Marco Aschwanden <PPNTWIMBXFFC (AT) spammotel (DOT) com> wrote:
| Quote: | 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
|
|
| Back to top |
|
 |
John Carson Guest
|
Posted: Mon Dec 29, 2003 11:53 am Post subject: Re: What are the header defines for? |
|
|
"Marco Aschwanden" <PPNTWIMBXFFC (AT) spammotel (DOT) com> wrote
| Quote: | 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)
|
|
| Back to top |
|
 |
EventHelix.com Guest
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|