6 Replies - 293 Views - Last Post: 15 September 2011 - 02:51 AM Rate Topic: -----

#1 Yatta!  Icon User is offline

  • D.I.C Head

Reputation: 16
  • View blog
  • Posts: 122
  • Joined: 06-September 09

Using Includes Properly?

Posted 15 September 2011 - 12:35 AM

I'm curious if there's a "proper" way to do includes when you're using header files. To explain what I mean I'll use an example:

Assume this is a header file named "head"... (I'm so original)
#ifndef GUARD_head
#define GUARD_head

#include <iostream>

// Random junk here

#endif



Now, seeing as I have <iostream> included in the header file, and assuming that I'm including "head.h" in main.cpp, is it "proper" (emphasis on proper, I already know I don't have to) to include <iostream> again in main.cpp?

This post has been edited by Yatta!: 15 September 2011 - 12:37 AM


Is This A Good Question/Topic? 1
  • +

Replies To: Using Includes Properly?

#2 PlasticineGuy  Icon User is offline

  • mov dword[esp+eax],0
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,436
  • Joined: 03-January 10

Re: Using Includes Properly?

Posted 15 September 2011 - 12:55 AM

If you need it, yes. It's always better to explicity include headers you need, even if a sub-header might include them anyway.

Also never include unnecessary headers inside other headers. iostream is a large library, and if you don't need it in the header you can cut down your code's size massively.
Was This Post Helpful? 1
  • +
  • -

#3 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1621
  • View blog
  • Posts: 3,079
  • Joined: 30-May 10

Re: Using Includes Properly?

Posted 15 September 2011 - 01:13 AM

> I already know I don't have to) to include <iostream> again in main.cpp?
I think you should.

Consider this counter example.
100 files all include head.h, but don't separately include iostream. Sometime later, you change head.h so that it is no longer dependent on iostream, so you remove it - result - massive build breakage. Rather than fix it properly (there's a deadline), you leave iostream stuck in list.h.

In a v.large project with 10,000+ source files, you can soon imagine how creeping useless dependencies can infest whole groups of files.

Each source and header should include everything it needs, and not be dependent on other files. So if either main.cpp or list.h stop using iostream, it shouldn't break the build.

> I'm curious if there's a "proper" way to do includes when you're using header files.
There are very few blanket rules. A counter-counter example would be to have a common.h which includes iostream and a few other regulars which most things seem to need. So long as you document it as such, and make sure everyone sticks to including common.h first, it's probably a good use.

Apart from that, just don't put using namespace std; inside any .h file.
Was This Post Helpful? 3
  • +
  • -

#4 Yatta!  Icon User is offline

  • D.I.C Head

Reputation: 16
  • View blog
  • Posts: 122
  • Joined: 06-September 09

Re: Using Includes Properly?

Posted 15 September 2011 - 01:35 AM

That's what I figured, thanks guys!

Quote

Apart from that, just don't put using namespace std; inside any .h file.


I assume it's because using namespace std; takes a lot of memory? Good thing I already didn't do it, then! My book just told me to type std:: manually in all of my headers :D
Was This Post Helpful? 0
  • +
  • -

#5 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1621
  • View blog
  • Posts: 3,079
  • Joined: 30-May 10

Re: Using Includes Properly?

Posted 15 September 2011 - 02:04 AM

Imagine a prog.cpp like this, with multiple namespaces.
#include <iostream>
#include <foo_conflicting_with_std.h>
#include <list.h>
...
std::cout << "hello";// uses the cout in iostream
foo::cout << "Yup";  // uses the cout in foo_conflicting_with_std.h


If list.h blindly uses using namespace, then there is nothing(?) prog.cpp can do about it (it certainly won't be pretty).

using namespace std; should just be a 1-line fix for old code
#include <iostream.h>


being updated to new code
#include <iostream>
using namespace std;


without the hassle of going through the rest of the code changing cout to std::cout

But new code in a world of different namespaces needs to keep things as tight as possible.
Was This Post Helpful? 1
  • +
  • -

#6 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2087
  • View blog
  • Posts: 3,175
  • Joined: 21-June 11

Re: Using Includes Properly?

Posted 15 September 2011 - 02:06 AM

View PostYatta!, on 15 September 2011 - 10:35 AM, said:

I assume it's because using namespace std; takes a lot of memory?


No, it doesn't take up any memory. The reason you shouldn't do it is that it pollutes the global namespace - not just of the header, but of any file including the header. And the more names there are in the global namespace, the more likely you are to get name conflicts.
Was This Post Helpful? 1
  • +
  • -

#7 Yatta!  Icon User is offline

  • D.I.C Head

Reputation: 16
  • View blog
  • Posts: 122
  • Joined: 06-September 09

Re: Using Includes Properly?

Posted 15 September 2011 - 02:51 AM

I see. Yeah, that makes total sense. Granted I don't know any other namespace, but that's no excuse to start a bad habit!

Thanks again, guys!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1