S
Steven T. Hatton
The reason Stroustrup warns against using set and get functions is that an
object of class type should be designed in such a way as to maintain some
invariant. All operations on the class should be such that they maintain
the invariant. An example is a std::vector<>. The invariant can be state
as: a std::vector<T> hold a number of elements given by the return value of
the member function std::vector<T>::size(). This means that adding or
removing elements from the vector must happen in such a way that the
returnvalue of size() reflects the resulting number of elements.
Hence, the value that should not be available for direct manipulation by a
user is the value returned by size(). We can assume for the sake of this
discussion that this return value is stored in some memory location defined
as a member field of the vector<> under discussion.
I find the general warning against using get and set member functions overly
vague, and potentially harmful. There are many, many, instances where
using such functions is perfectly legitimate, and can be done in such a way
as to preserve a meaningful invariant. Too often such general injunctions
become widely accepted in the same way as the misguidance rendered by the
injunction that a preposition is some thing we should not end a sentence
with has.
--
"If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true." - Bertrand
Russell
object of class type should be designed in such a way as to maintain some
invariant. All operations on the class should be such that they maintain
the invariant. An example is a std::vector<>. The invariant can be state
as: a std::vector<T> hold a number of elements given by the return value of
the member function std::vector<T>::size(). This means that adding or
removing elements from the vector must happen in such a way that the
returnvalue of size() reflects the resulting number of elements.
Hence, the value that should not be available for direct manipulation by a
user is the value returned by size(). We can assume for the sake of this
discussion that this return value is stored in some memory location defined
as a member field of the vector<> under discussion.
I find the general warning against using get and set member functions overly
vague, and potentially harmful. There are many, many, instances where
using such functions is perfectly legitimate, and can be done in such a way
as to preserve a meaningful invariant. Too often such general injunctions
become widely accepted in the same way as the misguidance rendered by the
injunction that a preposition is some thing we should not end a sentence
with has.
--
"If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true." - Bertrand
Russell