Considerations in C++ Method Definitions

In C++ you have a choice. You can either define a method directly in the class body, or you can define it outside the class. How do you decide?

There are a couple of items to keep in mind. The first is always readability. What makes it easier for the person reading the class description. In C++ this means you should put only very small methods, those having only one or two statements, in the class definition. Anything larger should be put in an implementation file.

In C++ there is also a semantics issue to consider. Methods defined in the class body may, at the discrection of the compiler, be expanded in-line. This means the body of the method will replace the call, and they will be executed much more efficiently. This is another reason why only very short methods should be defined this way. (But also why doing so might result in a dramatic improvement in performance).

[audio] [real] Text to accompany slide25, in Chapter 4 of An Introduction to Object-Oriented Programming