includes -- best form?

C

cppaddict

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
 
J

John Harrison

cppaddict said:
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
 
K

Karl Heinz Buchegger

cppaddict said:
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.
 
B

Bill Seurer

Karl said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,169
Messages
2,570,919
Members
47,460
Latest member
eibafima

Latest Threads

Top