including files in header vs implementation file (C++) -


what's difference between including header file in header file vs including in implementation file?

this

ex:

// test.h #include"globals.h"  class test {     test(); }; 

vs

//test.cpp #include"globals.h"  test::test() { } 

the general principle want minimise dependencies wherever reasonably possible, so:

  • if interface (.h) references in given header header needs #included in interface (.h)

  • if reference given header in implementation (.cpp) (and not in interface) should #include header in implementation

  • you should try #include headers needed, although can difficult maintain during lifetime large project

so example above, if don't reference globals.h in test.h, reference in test.cpp, #include should go in test.cpp. if reference globals.h in test.h though need #include in test.h.


Comments

Popular posts from this blog

node.js - Bad Request - node js ajax post -

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -