If I include a file in main that includes iostream. Will main contain iostream?

Started by
9 comments, last by Samsonite 18 years, 1 month ago
Hehe, there's no more to say [grin] EDIT: It will, right?
Hope I was helpful. And thank you if you were!
Advertisement
[edited out] yeah (if i understand you correctly)
If you mean like:

//Main.cpp#include "SomeHeaderThatIncludesSomeOtherHeader.h"void main(void){//bla}//SomeHeaderThatIncludesSomeOtherHeader.h#include <Iostream>


Then yes.
So if I do like this:

Main (contains) "Game.h" (which contains) "SDLManager.h" (which contains) "Globals.h" (which contains) "Includer.h" (which contains) <iostream>, <SDL/SDL.h> and <string>.

That would work?

Phew, that was a long list of includes [grin]
Hope I was helpful. And thank you if you were!
Quote:Original post by Samsonite
So if I do like this:

Main (contains) "Game.h" (which contains) "SDLManager.h" (which contains) "Globals.h" (which contains) "Includer.h" (which contains) <iostream>, <SDL/SDL.h> and <string>.

That would work?

Phew, that was a long list of includes [grin]


If I understand that list correctly, then yes, you will have access to whatever the Game Header File includes...
Yes, that would work.

The preprocessor goes through the code, and whenever it finds an #include <file> it's as if it copies and pastes the code in that header into the spot where it finds the #include. It doesnt matter how many includes you have, because it all effectively goes into one big file by the end.

With gcc there are options so that you can see the compiler output on just running the preprocessor, without doing any further processing. If your unsure about exactly how preprocessor directives work, its worth taking a glance.
"Leave it to the computer programmers to shorten the "Year 2000 Millennium Bug" to "Y2K." Isn't that what caused this problem in the first place?"
Quote:Original post by Samsonite
So if I do like this:

Main (contains) "Game.h" (which contains) "SDLManager.h" (which contains) "Globals.h" (which contains) "Includer.h" (which contains) <iostream>, <SDL/SDL.h> and <string>.

That would work?

Phew, that was a long list of includes [grin]

Yes. Which is why you should avoid unneccessary includes within headers. It creates unneccessary dependancies. Eventually, you end up in a situation where changing even a single header results in your entire project needing to be recompiled even though only one file really cares about the change. You should also not assume other files include things. If main requires std::string, then main.cpp should include <string>. If it gets included twice, oh well, that's what include guards are for. But if you end up removing <string> from some other header, you don't want to break main.cpp.

CM
If I recall, leaving such a long path of includes to get what you want might not be the best option. Altering some part on that path can lead to problems that might not be easy to figure out very quickly. Someone can correct me if I'm wrong.
Quote:Original post by Conner McCloud
... If it gets included twice, oh well, that's what include guards are for.
CM


You mean something like:

"#pragma once"?

Hope I was helpful. And thank you if you were!
or

#ifndef _FOO_H_
#define _FOO_H_

...

#endif

This topic is closed to new replies.

Advertisement