By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,234 Members | 1,941 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,234 IT Pros & Developers. It's quick & easy.

all code in .h file

P: n/a

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.
Jul 19 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
"Zimmen Gnauh" <ya*********@yahoo.com> wrote in message
news:m2************@yahoo.com...

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.
Jul 19 '05 #2

P: n/a
template classes need to be in .h files, so probably the project only
has template classes.
Zimmen Gnauh wrote:
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.


Jul 19 '05 #3

P: n/a
"Zimmen Gnauh" <ya*********@yahoo.com> wrote...
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

Jul 19 '05 #4

P: n/a
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
Jul 19 '05 #5

P: n/a
Ray Gardener wrote:
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.
Jul 19 '05 #6

P: n/a
"Ray Gardener" <ra**@daylongraphics.com> wrote in message
news:0PRsb.382917$6C4.8840@pd7tw1no...
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
--
jb

(replace y with x if you want to reply by e-mail)
Jul 19 '05 #7

P: n/a
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/RealtimeMa...dePatterns.htm

Sandeep
--
http://www.EventHelix.com/EventStudio
EventStudio 2.0 - Generate Sequence Diagrams and Use Case Diagrams in PDF
Jul 19 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.