8

If I have multiple files that #include each other, and all #include <iostream>, is this considered bad, and if so, how would I avoid it?

Benjamin Lindley
  • 101,917
  • 9
  • 204
  • 274
iobender
  • 3,346
  • 1
  • 17
  • 21
  • 3
    There's usually no problem doing so. Why do you want to avoid it? – πάντα ῥεῖ Jan 07 '14 at 19:42
  • Usually header files are guarded by the pre-processor – Ed Heal Jan 07 '14 at 19:43
  • It's fine, although doing it on a large scale can cause a circular dependency... (this can be "fixed" by putting the problematic #include in the cpp file. – Idov Jan 07 '14 at 19:43
  • @Idov - Should not if you use pre-processor guards – Ed Heal Jan 07 '14 at 19:49
  • @EdHeal: I don't think they will help if file1.h needs to know the classes in file2.h and file2.h needs to know the classes in file1.h – Idov Jan 07 '14 at 19:51
  • @idov - Forward declaration. The header file just includes the bare minimum and uses forward declaration If included multiple times the include guards just ignores the file – Ed Heal Jan 07 '14 at 19:54
  • @idov - Please fill in your details. Makes a better sense where you are coming from – Ed Heal Jan 07 '14 at 19:55
  • @EdHeal: forward declaration will only help if you don't instanciate the type from the problematic #include in your class. – Idov Jan 07 '14 at 19:58
  • @idov - Your statement does not make any sense – Ed Heal Jan 07 '14 at 20:25
  • @EdHeal: You can have a "Type*" member in your class and you can have a "Type" (not a pointer) member in your class. forward declartion will help only in the case of the pointer. – Idov Jan 07 '14 at 20:28
  • @idov - You are incorrect - Read Scott Myers book and try it. – Ed Heal Jan 07 '14 at 20:37

5 Answers5

15

No, there is nothing wrong with it. Every file that needs to directly use functionality from <iostream>, should include it directly. Header guards will take care of multiple inclusions.

There is a potential problem when you have circular dependencies. For example, see this question: Resolve header include circular dependencies

However, since <iostream> is unlikely to be including or depending on any of your files, circular dependencies are not a problem in this case.

Community
  • 1
  • 1
Benjamin Lindley
  • 101,917
  • 9
  • 204
  • 274
  • So it won't add the contents of multiple times? Don't files I write myself have to be guarded if they are included multiple times? – iobender Jan 07 '14 at 19:46
  • That's generally a good idea, but solves a different problem, namely that *your* symbols aren't defined multiple times. Even if you don't use guards in *your* headers, the `` is guarded by itself so that's not a problem for `` (and typically any other header from a library). – leemes Jan 07 '14 at 19:48
  • I see, so library files are automatically guarded? Thank you. – iobender Jan 07 '14 at 19:50
  • Not automatically, but typically libraries do this for you, so you won't have to care about multiple inclusions of *their* files; only for your own. I've never seen a library which doesn't use header guards except for files intended to be included multiple times for preprocessor hacking. – leemes Jan 07 '14 at 19:52
  • Before anyone misunderstands: the discussion above is about iostream being a header file and how it should have header guards preventing redundant inclusions in your code. However, you're not going to want to include iostream in your OWN headers! Only include in your header what your header itself is dependent on. iostream should generally only be included in your implementation file (.cpp/.cxx). If your header depends on the types in iostream, include iosfwd (which is smaller) in your header instead. For more info see: http://stackoverflow.com/questions/4300696/what-is-the-iosfwd-header – Mr.H Feb 27 '17 at 10:20
3

The first question is whether you really need to include iostream. In most cases headers don't really need iostream, but the lesser ostream (no need for cin, cout... just the type std::ostream& for operator<<). Even there, the correct header would be iosfwd which contains just forward declarations of those elements.

That is, of course, unless you need the full declarations for the types or the real iostreams... then just include them.

David Rodríguez - dribeas
  • 204,818
  • 23
  • 294
  • 489
1

No, this is not a problem. I have never heard at least of any.

Pre-processor should do the required work and I think it's also good style to have every class/sourcefile needing <iostream> to include it.

Therefor everyone knows that this file uses functionality provided by iostream.

By the way: using namespace std; should in any case be avoided to ensure everyone sees the corresponding used namespace.

Stefan
  • 2,603
  • 2
  • 33
  • 62
0

There's no problem is doing this, there is include guard, which ensures just one time inclusion of the standard header file

P0W
  • 46,614
  • 9
  • 72
  • 119
0

Not an issue as long as you make sure you don't have too many headers stacked in each other. Too many and certain OS can't handle it, namely older ones. But unless you got an ancient computer with very old software, it should be perfectly fine! Good Luck to you!

Evanias
  • 27
  • 11