C/C++ utility functions in H or Cpp ?

Started by
8 comments, last by Zahlman 15 years, 4 months ago
Hi As far as I know H file should be for function prototyping/declaring and CPP for implementation, but I often see professional code (for example some product open code libraries) where utility modules has only H files (i.e. function implementation is written also in H file) What is the difference between having implementation in cpp or h file? Is it something similar to C++ inline function idea? Thanks in advance
Advertisement
Small functions that are intended to be inlined tend to be placed in header files, so that their definition is available wherever they are used. This is done because compilers seldom have a link-time code generation feature that would allow inlining across translation units.
Quote:Original post by hexa
I often see professional code [...] where utility modules has only H files

Are you talking about functions, member functions, function templates or member function templates?
Quote:Original post by DevFred
Quote:Original post by hexa
I often see professional code [...] where utility modules has only H files

Are you talking about functions, member functions, function templates or member function templates?

Or even functions as macros?
I was talking about non-member functions, coding in C++ (but in C style - no classes)

Thanks for answers, I got it - templates are the reason...

So when coding in C/C++ template (and macros is obvious) functions are the only exception?




Putting functions in H files is much simpler for me as a programmer. I find myself writing entire classes (including the function implementations in h files.). It saves time and elliminates the need to switch back and forth between the cpp file and the h file every time I want to add/ modify a function.

The problem is that the compiler will have to go over the code in the H file for every "#include" command. If you are writing a big class/function that will be included many times through-out your source (Example: a math-library for a 3d game) your compile time will go up enourmously. This is because as mentioned before the compiler will have to go over the H file for every CPP that uses it.

So if you are going to write something for a big project avoid putting implementations in the H.

My Oculus Rift Game: RaiderV

My Android VR games: Time-Rider& Dozer Driver

My browser game: Vitrage - A game of stained glass

My android games : Enemies of the Crown & Killer Bees

All template functions must have their implementation in the *.h file, but that doesn't mean other implementations sometimes go there, too. There may be several reasons for that. If a function is only 1 or 2 lines long, it doesn't make sense to make an entry for it in the *.cpp file. You'll need your standard function comment header, and now your 1-2 line function takes up 20 lines of text instead of 2-3. Other times people do it as a way to make the compiler in-lines the function.

In general though, if the function is more than a couple lines long and not a templated function, it should not be in the *.h file.
Quote:Original post by SillyCow
Putting functions in H files is much simpler for me as a programmer. I find myself writing entire classes (including the function implementations in h files.). It saves time and elliminates the need to switch back and forth between the cpp file and the h file every time I want to add/ modify a function.


Except that it doesn't actually work, once you find that more than one .cpp file needs to call that function. You end up including the .h file in each, and end up with multiply symbol definition errors. Include guards do not address this, BTW. See more.
Quote:Original post by Zahlman
Quote:Original post by SillyCow
Putting functions in H files is much simpler for me as a programmer.

Except that it doesn't actually work

Unless he defines the functions as inline or static ;-)
Quote:Original post by DevFred
Quote:Original post by Zahlman
Quote:Original post by SillyCow
Putting functions in H files is much simpler for me as a programmer.

Except that it doesn't actually work

Unless he defines the functions as inline or static ;-)


I suppose. :)

This topic is closed to new replies.

Advertisement