• Advertisement
Sign in to follow this  

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

This topic is 4434 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Advertisement
If you mean like:


//Main.cpp
#include "SomeHeaderThatIncludesSomeOtherHeader.h"

void main(void)
{
//bla
}

//SomeHeaderThatIncludesSomeOtherHeader.h
#include <Iostream>





Then yes.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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"?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement