Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > ifndef define question

Reply
Thread Tools

ifndef define question

 
 
DrUg13
Guest
Posts: n/a
 
      02-07-2004
#ifndef HEADER_H
#define HEADER_H

blah blal
#endif

So this tells the compiler That if its defined do not define it again.
Could someone help me understand this.

Does this mean that if I use the same clasee in two different classes
in the same exe with out the above, that the exe would be larger
because the code would be linked twice, or basically put into the code
twice??
 
Reply With Quote
 
 
 
 
Jack Klein
Guest
Posts: n/a
 
      02-07-2004
On Sat, 07 Feb 2004 19:40:34 GMT, DrUg13 <> wrote
in comp.lang.c++:

> #ifndef HEADER_H
> #define HEADER_H
>
> blah blal
> #endif
>
> So this tells the compiler That if its defined do not define it again.
> Could someone help me understand this.


No, it means that if the preprocessor macro HEADER_H is already
defined, ignore the code up until the corresponding #endif. This
prevents the contents of a file from being processed more than once in
compiling the same source file even if it happens to be included more
than once.

> Does this mean that if I use the same clasee in two different classes
> in the same exe with out the above, that the exe would be larger
> because the code would be linked twice, or basically put into the code
> twice??


This usage, commonly called an "include guard", prevents the contents
in between the #ifndef and the #endif from being processed more than
once in any given source file. It will still be processed once only
for every individual source file in a program that includes it at
least once.

Note that proper usage for headers is that they should not define
actual variables or contain the bodies of any functions except for
inline member functions of classes. The definitions of type, such as
class definitions, and function prototypes do not in themselves
generate code or data.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
 
Reply With Quote
 
 
 
 
Rolf Magnus
Guest
Posts: n/a
 
      02-07-2004
DrUg13 wrote:

> #ifndef HEADER_H
> #define HEADER_H
>
> blah blal
> #endif
>
> So this tells the compiler That if its defined do not define it again.
> Could someone help me understand this.
>
> Does this mean that if I use the same clasee in two different classes
> in the same exe with out the above, that the exe would be larger
> because the code would be linked twice, or basically put into the code
> twice??


No. The above is a so-called include guard. When you include a header
file, the #include line is replaced by the contents of that header. But
in any translation unit, a class must not be defined more than once. A
simple example:

// a.h
class A
{
};


// b.h
#include "a.h"

class B
{
A a;
}


// main.c
#include "a.h"
#include "b.h"

int main()
{
A a;
B b;
}



This won't compile, because a.h is #included twice in main, once
directly and once through b.h, and as I said above, class A must not be
defined more than once in main.c. If you put in the include guard, the
first inclusion of a.h will #define (e.g.) A_H, and when it's included
the second time, that #define is found, and so the contents of the
header are now skipped up to the corresponding #endif.


 
Reply With Quote
 
Mike Wahler
Guest
Posts: n/a
 
      02-07-2004

"DrUg13" <> wrote in message
news:...
> #ifndef HEADER_H


This means that if, at this point in translation (which is sequential),
there is no symbol 'HEADER_H' defined, then translate everything between
here and the next (matching) #endif. If 'HEADER_H' is already defined,
then skip down to the next (matching) #endif before continuing to translate.

> #define HEADER_H
>
> blah blal
> #endif


>
> So this tells the compiler That if its defined do not define it again.


It means that if it's defined, then everything between the #ifndef
and #endif should be skipped over by the compiler, not translated.

> Could someone help me understand this.


See above.

>
> Does this mean that if I use the same clasee in two different classes
> in the same exe with out the above, that the exe would be larger
> because the code would be linked twice, or basically put into the code
> twice??


Nothing to do with 'exe' or linking. #ifdef, #ifndef, etc. are
'preprocessor
directives, which facilitate 'conditional compilation'.

The form above is often called a 'header guard' which allows one to
#include a header more than once in the same translation without
worry about duplications causing language violations.

-Mike


 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      02-07-2004

"DrUg13" <> wrote in message
news:...
> #ifndef HEADER_H
> #define HEADER_H
>
> blah blal
> #endif
>
> So this tells the compiler That if its defined do not define it again.
> Could someone help me understand this.
>
> Does this mean that if I use the same clasee in two different classes
> in the same exe with out the above, that the exe would be larger
> because the code would be linked twice, or basically put into the code
> twice??


No nothing like that. Look at this

// file header.h

class AClass
{
};

// file header2.h

#include "header.h"

// file source.cpp

#include "header.h"
#include "header2.h"

Now file source.cpp is including header.h *twice*, once directly and once
via header2.h. So the compiler is going to see the declaration of AClass
twice. That is an error, you can't declare a class twice in one compilation
(even if its the same definition both times). This is the error that the
include guard prevents. The second time that the header file is included it
has already defined HEADER_H so it #ifndef stops the compiler from seeing
the class declaration a second time ON THE SAME COMPILATION (this is the bit
that newbies usually don't understand). If you had another source file which
also included header.h then of course the compiler when separately compiling
that second source file would see the class definition again, but that's OK,
its not an error.

John


 
Reply With Quote
 
EventHelix.com
Guest
Posts: n/a
 
      02-08-2004
This #ifdef pattern is just to guard against multiple inclusions of
the same header file.

The following article will help:

http://www.eventhelix.com/RealtimeMa...dePatterns.htm

Sandeep
--
http://www.EventHelix.com/EventStudio
EventStudio 2.0 - Go Beyond UML Use Case and Sequence Diagrams
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
A question about ifndef QQ C Programming 19 11-04-2005 08:45 AM
How to use #ifndef #define, etc, in a macro definition Peng Yu C++ 6 10-03-2004 11:50 PM
#ifndef #define #endif and multiple definitions problem prettysmurfed C++ 3 10-24-2003 08:30 PM
#if !defined() vs #ifndef Evan C++ 4 07-12-2003 10:28 AM
Re: defined(name) vs #ifdef AND #ifndef Chris Torek C Programming 0 07-02-2003 01:28 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57