Jump to content
  • Advertisement
Sign in to follow this  
hexa

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

This topic is 3611 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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?




Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 ;-)

Share this post


Link to post
Share on other sites
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. :)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!