[prog] [C/C++] what to put in header files.

Hemingway, Neil neil.hemingway at barclays.co.uk
Mon Sep 23 09:44:05 EST 2002


Sonja,

> My understanding is that a header file should contain everything
> a "user" of my code needs to know without having to look at the
> code itself. So I would have function prototypes, typedefs, structs
> and (ouch) the odd global variable there.

Your understanding is correct (in principle).  The header file is the
public interface.  The .cpp is the private implemnetation of the
public interface.  If you were working in a multiteam situation where
your team provided stuff other teams needed, you might decide to only
give them the header files, and compiled objects for the implementation.
Indeed, I believe this is what several companies do.  (You have to worry
about which compiler was used, and what flags were set for the compilation,
but the theory is sound).

Of course, theory and practice don't often agree, and this is one such
situation.  You are sometimes forced to include things in the header
file that don't really belong there...

> I'm completely confused by the way C++ programmers do that, as method
> implementations turn up in header files quite often as well.

...Indeed.  Templates are one such situation.  They are (arguably) part
of the private implementation of a class / method, however, without being
able to see the template definition, the compiler won't be able to
compile code that instantiates the template.  Consequently, template
definitions often need to be in the header.


I think it's also worth referring you to my previous post for info about
the downside of superfluous header inclusions...

Regards, Neil



**********************************************************************
This email and any files transmitted with it are confidential and
intended solely for the use of the individual or entity to whom they
are addressed. If you have received this email in error please notify
the system manager.

This footnote also confirms that this email message has been swept by
MIMEsweeper for the presence of computer viruses.

www.mimesweeper.com
**********************************************************************


Internet communications are not secure and therefore the Barclays Group
does not accept legal responsibility for the contents of this message.
Although the Barclays Group operates anti-virus programmes, it does not
accept responsibility for any damage whatsoever that is caused by
viruses being passed.  Any views or opinions presented are solely those
of the author and do not necessarily represent those of the Barclays
Group.  Replies to this email may be monitored by the Barclays Group
for operational or business reasons.




More information about the Programming mailing list