![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Game engine designer
Join Date: May 2005
Location: Sweden
Posts: 301
Rep Power: 4
![]() |
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? |
|
|
|
|
|
#2 |
|
PFO God In Training
![]() Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 548
Rep Power: 4
![]() |
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 4:21 PM. |
|
|
|
|
|
#3 |
|
Expert Programmer
Join Date: Dec 2004
Posts: 794
Rep Power: 4
![]() |
unless you use "export"
but most compilers don't have that. |
|
|
|
|
|
#4 |
|
Programmer
Join Date: Jun 2005
Location: Maryland, USA
Posts: 59
Rep Power: 4
![]() |
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 |
|
|
|
|
|
#5 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,223
Rep Power: 5
![]() |
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. |
|
|
|
|
|
#6 |
|
Game engine designer
Join Date: May 2005
Location: Sweden
Posts: 301
Rep Power: 4
![]() |
So this means; put all inline and template code in header files, and anything else in cpp files.
/Thanks |
|
|
|
|
|
#7 |
|
Programmer
Join Date: Jun 2005
Location: Maryland, USA
Posts: 59
Rep Power: 4
![]() |
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 |
|
|
|
|
|
#8 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
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 |
|
|
|
|
|
#9 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#10 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
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 |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|