Question about classes in C++

Started by
1 comment, last by Verg 18 years, 8 months ago
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?
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.
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]

my_life:          nop          jmp my_life
[ Keep track of your TDD cycle using "The Death Star" ] [ Verge Video Editor Support Forums ] [ Principles of Verg-o-nomics ] [ "t00t-orials" ]

This topic is closed to new replies.

Advertisement