Jump to content
  • Advertisement
Sign in to follow this  
jlg

Question about classes in C++

This topic is 4853 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

I started learning C++ coming from a Java background. In Java I usually defined all the class functions inside the class. In the book I'm reading though, the author simply declares the class methods inside the class and defines them outside of it. He stated that defining the methods inside the class is ok too, but I was wondering if one way is better than the other. It makes more sense to me to define them inside the class, are there any drawbacks to this?

Share this post


Link to post
Share on other sites
Advertisement
In general, I would prefer to define the member functions outside the class in their own source file. Defining the member functions inside the class makes them implicitly inline. This can lead to code bloat as the functions get duplicated in the code that calls them. This also leads to coupling the implementation of the functions with the users of the functions. That means when you change the definition of the functions all the users of those functions need to be recompiled. For big projects this can take a lot of time. Finally, profilers are very good at telling you what functions should be inlined that weren't, but very bad at telling you what functions shouldn't have been inlined that were.

Share this post


Link to post
Share on other sites
Good points from the SiCrane :)

To add: if you ever want to release your code in a library, but don't want people mucking with the heart of your code (the implementation)... defining functions in a separate file is a good way to go. That way, those using your library can use the header file, but won't have access to your source file. That way, they won't get your latest programming secrets! [lol]

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!