"Should one pass back the variable as part of the class (e.g. T.c) or should one pass it back directly from a subroutine of that class via the subroutine return." |
Really, as pointed out before, you
should not store any "state" in member variables,
unless there is a good reason why this is necessary. This applies to "input" parameters as well as to return values. So, if you can pass parameters
directly into the function, and if you can return the result
directly, then
that is the way to go! The
indirection of making your function read its "input" parameters from member variables, or making your function write its result into a member variable, should be
avoided – unless there is a good reason to do otherwise.
Anyway, if you decide that your class needs to carry some "state" in its member variables, then you should implement a proper
encapsulation. This means that you should make your member variables
private and you also should initialize them in the
constructor of your class. Add "getter" and "setter" functions as needed.
Examples:
http://www.cplusplus.com/forum/beginner/283816/#msg1228862
In practice of course, the class could have numerous variables interacting in various was so there are reasons to store them in the class. |
Yes, there
can be good reasons to store member variables in the class.
But the question that you should always ask yourself is this: Is this variable a "state" of the object that needs to be maintained for the object's entire lifetime? If so, then a member variable is the right choice. If
not, then this probably should be a
direct function parameter, or a
direct return value,
not a member variable...