Home Forums Programming C++ question about header files Reply To: C++ question about header files

#36917
Anonymous
Inactive

Am I right in thinking that inlined functions are faster that declaring them in the cpp in certain cases? Don’t really believe it myself.
Check out Ogre3D, have whole functions and classes completely implemented in header files, its so nasty[/quote:e1bd94f4df]

They are slightly faster, but not that much faster. There’s a bit of extra overhead that comes with function calls, like pushing parameters and return addresses onto the stack, saving / restoring registers before and after a call, and one extra jump instruction. But it depends on what you’re doing really, how much of a benefit you’ll see from this. Basically if you’re calling a function lots of times within the body of a loop, then all that extra overhead will add up and you will see some benefit from inlining.

Just adopt the rule that if a function is only one or two statements long (such as get or set functions) then you should go inline, otherwise just declare it in the header and implement it in a .cpp file.

I don’t know why Ogre has the arrangement you mentioned above but it’s not really the best way to be going about things. Headers as you rightly pointed out before should only act as interfaces ( except in the case of simple inline functions and template functions / classes ) and should not contain implementations. They are a form of documentation on your classes if you think about it, so you should try and keep them as tidy as possible. Also, each time you change a header file then every .cpp file including that header must be re-compiled. So it’s better to try and put your implementations into the .cpp files whenever you can, just to save on your compile times.