Jonathan said:
Be careful when you make statements such as "this is stupid".
Sorry if it hurt you, I just remembered reading someone else's code
where for evry private member the author (probaly using macroses in
his/her text editor) put correstonding T getXXX(){return XXX;} void
setXXX(newXXX){XXX=newXXX;}. I think that person read somewhere that
it's good to have data members private
No. Data should be private when it may be dangerous for the objet's
state to modify it without validation. What's more, using member
functions to access an aggregated object separates implementation from
interface. If the aggregated objet changes, the "accessor" may be able
to make it transparent. Allowing clients to use what is called
"implementation details" makes the object harder to modify.
I think you rephrased my short sentence about "Private data should be
private...". Imagine, the agregated obect is some sort of private data,
and the object that stores it provides some methods that in some way
iteract (or don't) with it's private data... I really didn't understand
some parts of your message about separated interface and
implementation, and transparent chage of aggregate...
I personally never use public data, except for very simple cases (such
as a point class). Using member functions allows me to control the
access and to separate between interface and implementation.
Me too. I don't remeber when I used public data
A good example for public data would be complex numbers, where two
doubles (x,i) could be directly accessed. It would make sence to make
setters/getters for the two doubles only in case when we need to
calculate absolute value of the complex very often; In such case we
would have private member that stores absolute value and whenever x or
i gets new value we recalculate absolute value...
not sure if I'm right with termins. I meant absolute(complex) =
sqrt(x*x + i*i);
More complex examples will not add anything to the problem and will
more probably confuse the original poster. Well-written, simple
examples, even if they don't depict the whole spectrum of the language,
are usually easier to comprehend.
Well, example with complex numbers is really easy and tells where we
have benifit of using setters and private data.... Not only it has real
meaning (not like the A1 and A2 classes) but it's also very short
example.