For Programmers: Free Programming Magazines  


Home > Archive > VC Language > May 2006 > Good And Bad Effect of putting Class code in Header File.









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Good And Bad Effect of putting Class code in Header File.
MilanB

2006-05-31, 4:15 am

Hello,

Is good or bad solution to put whole class code in header, and avoid source
file (cpp). Does it have some specifics implications?

Thanks
Igor Tandetnik

2006-05-31, 8:15 am

"MilanB" <MilanB@discussions.microsoft.com> wrote in message
news:7B13989B-2491-407E-9137-DD766DF62340@microsoft.com
> Is good or bad solution to put whole class code in header, and avoid
> source file (cpp). Does it have some specifics implications?


Compilation time gets longer - the code gets recompiled over and over in
each source that includes your header.

Usability and maintainability suffer. With .h file containing just the
declaration, it serves as a reference to your class' methods. With
implementation mixed in, it becomes difficult to get an overview of the
class.
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925


Alex Blekhman

2006-05-31, 8:15 am

MilanB wrote:
> Is good or bad solution to put whole class code in
> header, and avoid source file (cpp). Does it have some
> specifics implications?


Usually it's bad solution because...

a) it exposes class' implementation to its user

b) it will require to include all necessary headers in
class' header instead of .cpp file hence increasing compile
time of user file

c) it increases coupling between classes; each change in
your class will require recompile of all its users

d) it is inconvenient to read such header if class has
less than trivial implementation

Unless you have tempalte class or absolutely must put all
implementation in header you should avoid it.



Bruno van Dooren

2006-05-31, 8:15 am

> Is good or bad solution to put whole class code in header, and avoid
> source
> file (cpp). Does it have some specifics implications?


You should only do this when designing template classes because there is no
other sensible option at the moment.

If you are creating a WinForms application you might wonder why the form
designer puts everything in the header file,
because that would seem to indicate that it is good practise to do so. It is
not.

The reason that the forms designer does this is that it is also used for C#
and VB. It does not understand the concept of
splitting declaration and implementation.

--

Kind regards,
Bruno van Dooren
bruno_nos_pam_van_dooren@hotmail.com
Remove only "_nos_pam"


Tom Serface

2006-05-31, 7:15 pm

As Bruno says, we're kind of stuck doing this with templates. Otherwise, I
only put code in the .h file for inline functions which are usually just get
and set variables that I don't want to be public. I think that doing
otherwise would work around the standard OOP paradigm that C++ encourages.
Sometimes I think C++ is just a little too flexible for its own good :o) or
ours.

Tom

"MilanB" <MilanB@discussions.microsoft.com> wrote in message
news:7B13989B-2491-407E-9137-DD766DF62340@microsoft.com...
> Hello,
>
> Is good or bad solution to put whole class code in header, and avoid
> source
> file (cpp). Does it have some specifics implications?
>
> Thanks



Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com