ifndef define question

D

DrUg13

#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??
 
J

Jack Klein

#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.
 
R

Rolf Magnus

DrUg13 said:
#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.
 
M

Mike Wahler

DrUg13 said:
#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
 
J

John Harrison

DrUg13 said:
#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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top