Steven T. Hatton wrote:
Personally, I'm finding all this jumping back and forth between class.cpp and class.h rather burdensome, and I'm wondering why I should even do it. Why not just do everything within the class body and be done with it?
Sometimes this makes sense. If the method finds the maximum of two integers, then I would put it in the class body because that's efficient. On the other hand, if the method reads an XML file and checks that it is valid, I would separate the definition from the declaration because the definition is sure to be very long and the method might be used in many subclasses. If I kept the definition in the class body, the object code would become very big because it would have many copies of the method rather than just one. Usually there's a tradeoff between speed and size. Small methods are best defined in the class body because the code runs faster (fewer function calls) but isn't much bigger. Big methods are best defined outside because the function call overhead is small compared to the work the function does and it reduces object code bloat (and makes the code compile faster).
Whether a function should be inline or not depends on many factors.
I would like to hear your views on this. From my Java experience, it seems rather strange to define things about a class outside of the body.
-- JDL