By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,918 Members | 2,294 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,918 IT Pros & Developers. It's quick & easy.

includes -- best form?

P: n/a
When the methods of your class require include files (eg, one of your
functions might need the <algorithm> include), is it considered proper
form to put all of the includes in the header file, and simply include
the header file in the .cpp implementation file, or should the
includes go in the .cpp file?
thanks for any advice,
cpp
Jul 22 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a

"cppaddict" <he***@hello.com> wrote in message
news:k9********************************@4ax.com...
When the methods of your class require include files (eg, one of your
functions might need the <algorithm> include), is it considered proper
form to put all of the includes in the header file, and simply include
the header file in the .cpp implementation file, or should the
includes go in the .cpp file?


If <algorithm> is needed to make the header file compile put it in the
header file, otherwise in the implementation file.

A simple way to make sure that you follow this rule is to always put a
header file as the very first include in its implementation file, then any
missing headers in the header file will always show up as compile errors.

john
Jul 22 '05 #2

P: n/a
cppaddict wrote:

When the methods of your class require include files (eg, one of your
functions might need the <algorithm> include), is it considered proper
form to put all of the includes in the header file, and simply include
the header file in the .cpp implementation file, or should the
includes go in the .cpp file?


In header files you include only the things which are *needed*
in the header file.

If your cpp needs eg. <algorithm>, then it is the responsibility
of the cpp to include that.
--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #3

P: n/a
Karl Heinz Buchegger wrote:
In header files you include only the things which are *needed*
in the header file.


BTW, you really need to be careful about that. Over time your header
(and other) files tend to accumulate #includes for things they really
aren't using.

One customer group of an old compiler I worked on had many problems
because essentially any time you included any of their header files you
got them all. It was a HUGE project so even trivial source files took a
long time to compile and big source files sometimes took hours! And it
was pre-namespace days so all the includes cluttered up the name space too.
Jul 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.