22

If I have a class A which uses iostream, should I put the include statement of iostream in A.h or A.cpp?

user695652
  • 4,105
  • 7
  • 40
  • 58

5 Answers5

29

This is an area of some controversy. My own preference is that each header should be able to stand alone, so if it needs other headers, it includes them. In other words, if client code is going to need to include <iostream> (or whatever) anyway, your header should handle that for them. OTOH, if the user of the iostream is strictly hidden so the client code doesn't need to include it at all, then it should only be included in the implementation file.

In many cases (especially where the header is open to frequent change), you'd prefer to avoid including it in the header. In such cases, the PImpl idiom can be useful to get the dependency out of the header.

If you do need to include <iostream>, do your clients a favor and consider whether you can #include <iosfwd> instead of <iostream> though. This can improve compile time a fair amount.

R. Martinho Fernandes
  • 228,013
  • 71
  • 433
  • 510
Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • 4
    ooohhh let's not upvote you to keep you at 99998 rep. Perhaps, you can un-downvote one answer yourself ?! :) (just kidding +1) – sehe Oct 11 '11 at 22:22
  • @sehe: thanks. If it had been 99999, I'd have like to at least see it before the upvote, but as it is the palindrome was ruined anyway. Hm...maybe I just need to (at least temporarily) throw out 9 down-votes... :-) – Jerry Coffin Oct 11 '11 at 22:26
  • 3
    Re *My own preference is that each header should be able to stand alone, so if it needs other headers, it includes them.* Yes, but a header shouldn't `#include` a roman-sized bathhouse (``) when it only needs a tiny little washbasin (``). – David Hammen Oct 11 '11 at 22:36
  • @DavidHammen: Yes, I pointed out the same. Definitely worth repeating though. – Jerry Coffin Oct 11 '11 at 22:46
4

Include it where its needed. If you use something defined in <iostream> in the declaration of the class (like a member variable, a member function parameter or return type, etc), then it should be in the H file. If you only use it in the implementation - then in the CPP.

littleadv
  • 20,100
  • 2
  • 36
  • 50
3

Include it in the cpp. That way it's not potentially included in other cpp files that may include your A.h but don't need the iostream. Unless of course for some reason there is something in your header file that needs iostream. But if that's the case you might be doing something else wrong...

Kurtis Nusbaum
  • 30,445
  • 13
  • 78
  • 102
1

It depends.

If you use the classes in the header file, you need it in the header file (obviously).

If you just use the class declarations you can use incomplete types. In that case, include <iosfwd> in your header file, and <iostream> in the cpp

James Adkison
  • 9,412
  • 2
  • 29
  • 43
sehe
  • 374,641
  • 47
  • 450
  • 633
1

Use it where it is needed.

If your class declaration references types in the header, you will need to include it there. If it's only in the implementation, then you can include it in the cpp file.

Joe
  • 41,484
  • 20
  • 104
  • 125