C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

What are the header defines for?

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Marco Aschwanden
Guest





PostPosted: Mon Dec 29, 2003 10:53 am    Post subject: What are the header defines for? Reply with 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
Back to top
Sam Holden
Guest





PostPosted: Mon Dec 29, 2003 11:00 am    Post subject: Re: What are the header defines for? Reply with quote



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





PostPosted: Mon Dec 29, 2003 11:53 am    Post subject: Re: What are the header defines for? Reply with quote



"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





PostPosted: Tue Dec 30, 2003 3:31 am    Post subject: Re: What are the header defines for? Reply with quote

The following article explains duplicate inclusion and other
rules related to header file inclusion in C++.

http://www.eventhelix.com/RealtimeMantra/HeaderFileIncludePatterns.htm

Sandeep
--
http://www.EventHelix.com/EventStudio
EventStudio 2.0 - Go Beyond UML Use Case and Sequence Diagrams
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.