Path: utzoo!mnetor!uunet!husc6!cmcl2!brl-adm!umd5!mimsy!chris From: chris@mimsy.UUCP (Chris Torek) Newsgroups: comp.lang.c++ Subject: access functions Message-ID: <11152@mimsy.UUCP> Date: 21 Apr 88 06:03:31 GMT References: <8063@apple.Apple.Com> <6590039@hplsla.HP.COM> Organization: U of Maryland, Dept. of Computer Science, Coll. Pk., MD 20742 Lines: 48 In article <6590039@hplsla.HP.COM> jima@hplsla.HP.COM (Jim Adcock) writes: >If you refuse to stick to your original definition of blah, >and you make blah public, and someone uses blah, and then you change >your definition of what blah means, then that user of your class is >going to be hosed. > >BUT this is true whether or not blah is an access function or a public >instance variable. If you change your definition of what a public >anything means, you will hose your users. This is true. The point, however, is that a variable is always a variable, while a function is arbitrary. So if you declare that henceforth and forever, anyone can take complexvar.realpart() and complexvar.imaginarypart(), and you decide you prefer polar coordinates, people can still take the two, while if you declare that anyone can simply take .realpart and .imaginarypart, you are stuck with rectangular. To make the example slightly more interesting, consider a system in which complex numbers are needed in rectangular form about half the time, and in polar about half, but `clustered'. Then if you used access functions you can write something like class complex { union { struct { double re, im; } r; // if rectangular struct { double r, theta; } p; // if polar } u; boolean polar; // true => polar, else rectangular void flip(); // guess... public: inline double re() { if (this->polar) flip(); return (this->u.r.re); } inline double theta() { if (!this->polar) flip(); return (this->u.p.theta); } // etc } which has the nice side effect of leaving it in the last-used format, eliminating unnecessary conversions. If you never need the polar representation, this is just excess baggage. The question becomes `do I want to leave myself the option of using polar representation?' If you intend to reuse the implmentation later, perhaps it would be wise to do so. -- In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163) Domain: chris@mimsy.umd.edu Path: uunet!mimsy!chris