"DrUg13" <li******@dieyoung.com> wrote in message
news:9p********************************@4ax.com...
#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