Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > What is the benefit of putting a class definition in a header file.

Reply
Thread Tools

What is the benefit of putting a class definition in a header file.

 
 
DaTurk
Guest
Posts: n/a
 
      01-24-2007
Hi,

I'm a bit rusty with the wonderful language of c++. I worked with it,
maybe 6 or 7 years ago and now I need to knock the dust off. But I was
perusing some code when I noticed that this one programmer put the
entire class in the header file. I'm not sure what the point to that
is. Can someone please enlighten me as to why you would want to do
this?

And if you do want to, what are the pro's and cons of doing it. Are
there times you don't want to do this?

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      01-24-2007
DaTurk wrote:
> I'm a bit rusty with the wonderful language of c++. I worked with it,
> maybe 6 or 7 years ago and now I need to knock the dust off. But I
> was perusing some code when I noticed that this one programmer put the
> entire class in the header file.


What do you mean by "entire"? With member function _definitions_?
Lacking the clarity, I'll just assume that it's what you meant.

Just to let you know: if you see a curly brace after the class name
(or after the base class[es] name[s]), it's a class *definition*.
It ends with a closing curly brace and usually a semicolon. When
you talk of member function definitions, then they collectively are
called "class implementation". A class *definition* can (allowed to)
only contain member function *declarations*.

> I'm not sure what the point to that
> is. Can someone please enlighten me as to why you would want to do
> this?
>
> And if you do want to, what are the pro's and cons of doing it. Are
> there times you don't want to do this?


In most cases if the functions are small, letting them be defined in
the class definition is a way to make them 'inline' implicitly thus
allowing the compiler to produce faster code. It is also a way to
keep everything close-by, and not have an extra translation unit to
compile (and link with).

The most commonly quoted drawback of putting the implementation in
the header is that every time you need to change it, other modules
(translation units) need to be recompiled. So, for real-world large
systems, it's more common to keep the implementation details in the
header to a minimum.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
 
 
 
=?ISO-8859-1?Q?Erik_Wikstr=F6m?=
Guest
Posts: n/a
 
      01-24-2007
On 2007-01-24 19:56, DaTurk wrote:
> Hi,
>
> I'm a bit rusty with the wonderful language of c++. I worked with it,
> maybe 6 or 7 years ago and now I need to knock the dust off. But I was
> perusing some code when I noticed that this one programmer put the
> entire class in the header file. I'm not sure what the point to that
> is. Can someone please enlighten me as to why you would want to do
> this?
>
> And if you do want to, what are the pro's and cons of doing it. Are
> there times you don't want to do this?



Usually you don't want to put more than the declaration in the header-
file since this allows you to compile the definitions separately and
avoids having to recompile any file that includes the header file when
the implementation (but not the interface) changes.

However for some code you must put some or all in the header-file. When
dealing with templates the full definition must be available to the file
that includes your header, and the definition must thus also be in the
header-file (or a file included by the header-file).

Personally I don't see any advantage of putting the code in the header
unless you have to, or if the code is very small, so the work associated
with having to separate the declaration from definition is relatively
large (it's never much work).

A disadvantage would be that it can increase compile-time, for large
projects the difference can be quite much.

--
Erik Wikström
 
Reply With Quote
 
Michael
Guest
Posts: n/a
 
      01-24-2007
> The most commonly quoted drawback of putting the implementation in
> the header is that every time you need to change it, other modules
> (translation units) need to be recompiled. So, for real-world large
> systems, it's more common to keep the implementation details in the
> header to a minimum.


I'll agree with Victor's pros. One more pro: it's one less file to
deal with.

On the cons, there's one more - it might introduce additional
dependencies. For example, let's say I have a class like this:

class MyClass {
<bunch of stuff missing>
double GetComplicatedFunction() const {
/* do some complicated function that involves classes X, Y, Z.
*/
}
}

Now, if I put the implementation in the header file, I have to include
X.h, Y.h, and Z.h there. Then any class that uses MyClass has
dependencies on X.h, Y.h, and Z.h. If, on the other hand, I put the
implementation in the .cpp file, and only the declaration in the .h
file:

class MyClass {
<bunch of stuff missing>
double GetComplicatedFunction() const;
}

in this case, MyClass.cpp still needs to include X.h, Y.h, and Z.h, but
lots of other files don't need to include it recursively. So if you
change something in X.h, you wouldn't need to recompile nearly as much,
whereas in the first case, you'd have to recompile everything that used
MyClass.

This kind of thing really gets bad when you have larger projects, and
there's a huge benefit to minimizing dependencies.

Michael

 
Reply With Quote
 
red floyd
Guest
Posts: n/a
 
      01-24-2007
Michael wrote:
> On the cons, there's one more - it might introduce additional
> dependencies. For example, let's say I have a class like this:
>
> class MyClass {
> <bunch of stuff missing>
> double GetComplicatedFunction() const {
> /* do some complicated function that involves classes X, Y, Z.
> */
> }
> }
>
> Now, if I put the implementation in the header file, I have to include
> X.h, Y.h, and Z.h there. Then any class that uses MyClass has
> dependencies on X.h, Y.h, and Z.h. If, on the other hand, I put the
> implementation in the .cpp file, and only the declaration in the .h
> file:
>
> class MyClass {
> <bunch of stuff missing>
> double GetComplicatedFunction() const;
> }
>
> in this case, MyClass.cpp still needs to include X.h, Y.h, and Z.h, but
> lots of other files don't need to include it recursively. So if you
> change something in X.h, you wouldn't need to recompile nearly as much,
> whereas in the first case, you'd have to recompile everything that used
> MyClass.
>
> This kind of thing really gets bad when you have larger projects, and
> there's a huge benefit to minimizing dependencies.
>


The technical term is "unnecessary coupling" and it's a major
maintenance and unit testing nightmare.
 
Reply With Quote
 
NagelBagel@gmail.com
Guest
Posts: n/a
 
      01-25-2007
You can't put normal member function declarations in the header, unless
they're template definitions or inlined. Otherwise, you'll have
multiple definitions of the same function and get a linker error.

 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      01-25-2007
wrote:
> You can't put normal member function declarations in the header, unless
> they're template definitions or inlined. Otherwise, you'll have
> multiple definitions of the same function and get a linker error.
>

Not if they are within a class/struct declaration. There they are
implicitly inline.

--
Ian Collins.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Separate Template Definition I wrote class Data in header. The C++Compiler compiled without errors. I decided to move all member functionsinto source code because they are for implementation. I do not like thatthey are placed in class body. Immortal Nephi C++ 12 07-30-2010 11:54 AM
Header files with "header.h" or <header.h> ?? mlt C++ 2 01-31-2009 02:54 PM
Class.cast(Object) - what benefit? Ian Pilcher Java 16 08-23-2005 09:19 PM
can a class definition inside another class's definition Jianli Shen C++ 1 03-13-2005 06:02 PM
Difference between putting code in constructor and putting code in static{} Saurabh Java 6 05-30-2004 02:44 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57