'#ifndef' how is this working?

Started by
3 comments, last by when4 21 years, 2 months ago
ok, i should know this by now, but i dont haha, i got a tutorial on how to display multiple bitmaps and i dunno how all the files are linked together. i would always add "#include 'whatever.cpp'" in all of my files if i had more than one. but they have this... 'displaybitmaps.cpp' #include "bitmap.h" 'displaybitmaps.h' #ifndef _BITMAP_H #define _BITMAP_H 'bitmap.cpp' #include "bitmap.h" 'bitmap.h' #ifndef _BITMAP_H #define _BITMAP_H now these are all of the defines and includes(except windows.h and such). so how are all of these files linked together? i looked all over msdn and couldnt figure it out. [edited by - when4 on January 30, 2003 3:30:40 PM]
Advertisement
In pseudo it''s like this

  if not defined _BITMAP_H {  define _BITMAP_H  //some stuff here}//now if someone tested again for "if not defined _BITMAP_H" it would be defined and the code would be skipped  

Which makes the "some stuff" to be only once in the code.
So in case someone does this:

#include "bitmap.h"
#include "bitmap.h"
#include "bitmap.h"

The contents of bitmap.h will only be included once and not 3 times.

I don''t know why displaybitmaps.h has the same defines as bitmap.h (_BITMAP_H). That''s probably a typo. The defines should be different, otherwise if you do this:

#include "bitmap.h"
#include "displaybitmaps.h"

it will only include what''s inside bitmap.h, not what''s inside displaybitmaps.h (since by the time it reaches displaybitmaps.h, _BITMAP_H is already defined). And simply changing the order

#include "displaybitmaps.h"
#include "bitmap.h"

Will make only displaybitmaps.h''s contents to be included. This is most likely undesirable.

By the way, there''s also #endif at the end of your header files. That''s the same as ''}''-character for normal if clause.
Well, I'm not sure if that was your question but I use them like this:

(Creating some .cpp files that both need the same include)

main.cpp (edited by user)
engine.cpp (holds engine)

the header file that's included in both is engine.h (this is needed in main.cpp for the functions of this fake engine)

If you include the header in both files you get errors, but if you make the header like this:

#ifndef _ENGINE_H_ // (may have another name, just choose a name that doesn't give problems)
#define _ENGINE_H_

// Do all your header stuff

#endif

This will make sure that variabeles are not defined twice if the header is included in both files, so you don't get the errors. But what's the use then? Well, I don't know how, but somehow the .cpp file where the header is included second (so the last called file) also reads in the variabele names and functions etc, so you can still use them in your .cpp file

Well there's a little more to it than that I think, but that's it (btw #ifndef means "if not defined" or something )

Edit:
Sorry for saying things twice unnecessarily... the "post" page didn't work for a few times...

[edited by - Subotron on January 30, 2003 3:02:55 PM]
yes, thats what i wanted to know Subotron.
so its just a way to include a file multiple times.

but also, when i use multiple files in a program i write...

''DisplayBitmaps.cpp''
#include "Bitmaps.cpp"

''Bitmaps.cpp''
#include "Functions.cpp" //or whatever other files i would have.

and so on, so how is ''bitmaps.cpp'' included into the program?
you have to do #endif to close the section

This topic is closed to new replies.

Advertisement