Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jun 14th, 2005, 5:15 PM   #1
Klarre
Game engine designer
 
Klarre's Avatar
 
Join Date: May 2005
Location: Sweden
Posts: 314
Rep Power: 4 Klarre is on a distinguished road
Header files

Why do some people write their function implementations in the header files?
I can't see any good reason doing that. I think the header files should just be used as a list of the member functions in the class, so you can easily see which functions that are available.

Is there anyone out there who can tell me why this "java style" coding is used by some people?
Klarre is offline   Reply With Quote
Old Jun 14th, 2005, 5:18 PM   #2
Ancient Dragon
PFO God In Training
 
Ancient Dragon's Avatar
 
Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 599
Rep Power: 4 Ancient Dragon is on a distinguished road
I put only in-line functions/methods in header files. And inline functions/methods are only one or two statements.

Templates are different -- most templates are implemented in header files because the data type is unknown when the template is written so its not really possible to put the code into *.cpp files.

Last edited by Ancient Dragon; Jun 14th, 2005 at 5:21 PM.
Ancient Dragon is offline   Reply With Quote
Old Jun 14th, 2005, 7:59 PM   #3
uman
Expert Programmer
 
Join Date: Dec 2004
Posts: 794
Rep Power: 4 uman is on a distinguished road
unless you use "export"

but most compilers don't have that.
uman is offline   Reply With Quote
Old Jun 15th, 2005, 6:56 AM   #4
mitakeet
Programmer
 
mitakeet's Avatar
 
Join Date: Jun 2005
Location: Maryland, USA
Posts: 59
Rep Power: 4 mitakeet is on a distinguished road
Some compilers will troll through .cpp files looking for things to inline (Intel's compiler actually trolls through the binaries), but if you want maximum portablity, you will put in-linable code (and templates) in headers.
__________________

Free code: http://sol-biotech.com/code/.

It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
--Mitakeet

The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
--George Bernard Shaw
mitakeet is offline   Reply With Quote
Old Jun 15th, 2005, 9:21 AM   #5
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,260
Rep Power: 5 grumpy will become famous soon enough
Apart from usage of templates (few compilers support export, so the implementation has to be in the header), the most common reasons (in my experience) to put a file implementation in a header are;

1) they want the function to be inline. The compiler can potentially squeeze a bit more performance by inlining the function. YMMV, as the compiler can still decide not to inline a function (and the benefits are also compiler dependent). Simpler functions have most chance of being inlined.

2) laziness - it is easier to create one file rather than two for the same thing. If you don't care about maintainability, recompile times, etc etc.
grumpy is offline   Reply With Quote
Old Jun 15th, 2005, 9:24 AM   #6
Klarre
Game engine designer
 
Klarre's Avatar
 
Join Date: May 2005
Location: Sweden
Posts: 314
Rep Power: 4 Klarre is on a distinguished road
So this means; put all inline and template code in header files, and anything else in cpp files.

/Thanks
Klarre is offline   Reply With Quote
Old Jun 15th, 2005, 9:28 AM   #7
mitakeet
Programmer
 
mitakeet's Avatar
 
Join Date: Jun 2005
Location: Maryland, USA
Posts: 59
Rep Power: 4 mitakeet is on a distinguished road
What this means is it is a convention where you put the code, though for most compilers if you want the chance for in-lining it must be in the header.
__________________

Free code: http://sol-biotech.com/code/.

It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
--Mitakeet

The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
--George Bernard Shaw
mitakeet is offline   Reply With Quote
Old Jun 15th, 2005, 10:42 AM   #8
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
How would header files make recompile times faster? The compiler is going to compile both aint it? So accessing a header file would even make it slower, this would be very unnoticable though.
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for."
-- Socrates
nnxion is offline   Reply With Quote
Old Jun 15th, 2005, 10:47 AM   #9
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Precompiled headers.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Jun 15th, 2005, 10:52 AM   #10
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
Aww yeah never really worked with them, maybe because I don't recompile all that often .
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for."
-- Socrates
nnxion is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 1:56 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC