Hi guys,
I have two include related questions because I sometimes run into include problems and only get them solved very very diffecult and time (solving-time)-consuming.
I leaned C/C++ in school but it was only very basic stuff, nothing realy complicated.
For my job I had to switch to an other language and did C/C++ only seldom in my freetime.
I red this article about includes:
http://cplusplus.com/forum/articles/10627/
But even with the techniques there I got some problems a few month ago :/ (but thankfully more seldom since i red it^^ )
My questions are:
Do the include guards realy only compile one time for a WHOLE program? I only find yes as awnser in other forum searches.
But why are the already executed includes important to include in other files again? (Or is it only for IDEs?)
Example:
X.h
1 2 3 4 5 6 7 8 9 10 11
|
#ifndef X_H
#define X_H
#include "A.h"
#include "B.h"
class X {
A a;
B b;
};
#endif
|
Y.h
1 2 3 4 5 6 7 8 9 10 11
|
#ifndef Y_H
#define Y_H
#include "A.h"
#include "C.h"
class Y {
A a;
C c;
};
#endif
|
main.cpp
1 2 3 4 5 6 7 8 9 10 11
|
#include "X.h"
#include "Y.h"
#include "A.h"
int main() {
X x;
Y y;
A a;
return 0;
}
|
Assuming that the A, B and C header files have include guards too...
Means that the include guard of A protects the include again in Y.h and the last include in main.cpp? But why I need to include it in Y.h again for example.
And...
In the article there is a point where the author mentions that including any header files in source files is save moast time... but why?
Maybe I did not understand this article completly in case of a little language barrier but I think I got the moast things^^
Would be nice if anyone of you could help me out with this.