all code in .h file

A

Ali R.

Zimmen Gnauh said:
Is there any criteria regarding where the code should be placed, .h
file or .cc (.cpp) file? I recently came across a project
(http://cvs.sourceforge.net/viewcvs.py/slate/slate/) where all the
code is placed in .h files, there is no single .cc (.cpp) file! I'm
wondering whether there is any obvious advantage of doing this.

the classes in Slate seemed to be all Template classes. The implemetion of
methods in a template class must be in the .h file. And also inline
functions must be in .h file. Everything else being in the .h file in my
opinion is bad programming practice.

Ali R.
 
A

amit gulati

template classes need to be in .h files, so probably the project only
has template classes.
 
V

Victor Bazarov

Zimmen Gnauh said:
Is there any criteria regarding where the code should be placed, .h
file or .cc (.cpp) file? I recently came across a project
(http://cvs.sourceforge.net/viewcvs.py/slate/slate/) where all the
code is placed in .h files, there is no single .cc (.cpp) file! I'm
wondering whether there is any obvious advantage of doing this.

Names do not matter. If your compiler compiles files with
extension '.h', place your code there if you want. However,
you cannot have all headers and no compilable source unless
your product is a library (like a template library, e.g.)
and is not supposed to exist as a runnable program.

Victor
 
R

Ray Gardener

Code in header files will run faster since it is intrinsically inlined but
will bloat your program's size since each function call will resolve to a
copy of the function's code. This also means that recursive functions cannot
be (or take extra care to code) in headers (see recursive function templates
for examples). The speed increase is related to function call overhead,
however, so only small functions will be significantly improved.

If you are developing a library for binary release, code in header files
will be publicly visible, so the more code that is there, the less secret
your implementation becomes.

Ray
 
L

lilburne

Ray said:
The speed increase is related to function call overhead,
however, so only small functions will be significantly improved.

But not all small functions will be significantly improved,
and those that are will need to be called 1000s perhaps
1,000,000 of times before you'll be able to measure the
significance.
 
J

Jakob Bieling

Ray Gardener said:
Code in header files will run faster since it is intrinsically inlined but

That depends. As far as I know, the following function will not be
inlined, even though it is in the header

--- test.h ---

class my_test
{
void func ();
};

my_test::func () {}

--- end test.h ---

because it is missing the implicit or explicit inline specifier.
will bloat your program's size since each function call will resolve to a
copy of the function's code. This also means that recursive functions cannot
be (or take extra care to code) in headers (see recursive function
templates

Whether function code is inside the header or the cpp file does not
affect whether or not it can be recursive or not. No extra care needs to be
taken for a recursive function inside a header. It's the compiler's
responsibility to compile the code and stop (or not allow) inline recursion
whenever necessary.

regards
 
E

EventHelix.com

The problem with putting all the code in header files is that
increases the dependencies involved in including a header file.

Thus any file including an object gets all the header file
depencies of the class. When the code is placed in h and cpp files,
including the header file adds depencies only for variables that
are *required* for the header file.

See the following article to clarify this:

http://www.eventhelix.com/RealtimeMantra/HeaderFileIncludePatterns.htm

Sandeep
 

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

No members online now.

Forum statistics

Threads
474,146
Messages
2,570,832
Members
47,374
Latest member
EmeliaBryc

Latest Threads

Top