Path: utzoo!mnetor!uunet!husc6!bloom-beacon!tut.cis.ohio-state.edu!osu-cis!att!ihnp4!ihlpf!nevin1 From: nevin1@ihlpf.ATT.COM (00704a-Liber) Newsgroups: comp.lang.c++ Subject: Re: access functions Message-ID: <4660@ihlpf.ATT.COM> Date: 6 May 88 01:06:51 GMT References: <11152@mimsy.UUCP> <6590046@hplsla.HP.COM> Reply-To: nevin1@ihlpf.UUCP (00704a-Liber,N.J.) Organization: AT&T Bell Laboratories - Naperville, Illinois Lines: 57 In article <6590046@hplsla.HP.COM> jima@hplsla.HP.COM (Jim Adcock) writes: |Not strictly true. It is not uncommon for the compiler to assign |["unnecessarily"] to an intermediate variable in order to "make sure" |that the value of the underlaying variable that the access function |protects does not "accidentally" get changed. The compiler seems to |do this whenever it is unsure whether or not that underlaying variable |might get clobbered. Yes, but in this case I would expect the underlying C compiler to optimize this out. For the moment, let us say that given a very good optimizing compiler, there should be no extra run-time performance penalty for providing access functions for simple assignments. |Again, my only point is that I do not believe one should say uncategorically |that access functions should be used everywhere. Someone try to explain |to me what great advantages access functions have in the following |[trivialized to the ridiculous] example ???: |class IntVal |{ | private: | int val; | public: | void setValue(int aval) {val = aval;} | int value() {return val;} |}; |verses |class IntVal |{ | public: | value; |}; Suppose that, after writing a couple of applications using IntVal, you decide that you want more precision, say 9*sizeof(long) bytes worth of precision. With your method, this cannot be done without rewriting all the code dependent on IntVal. If access functions were used for the assignments, none of the code dependent on IntVal (except for the actual coding of the access functions themselves, of course) would need to be changed. |But if even this one example shows that "use access functions everywhere" |statement is overreaching, haven't I shown its converse, namely that there |ARE situations where one should consider NOT using access functions ??? I don't think so. By not using access functions, you lock yourself into a single specific implementation of the class, and this defeats the whole point of programming using data abstraction techniques. If you are going to do this, what do you gain by using C++ instead of C? -- _ __ NEVIN J. LIBER ..!ihnp4!ihlpf!nevin1 (312) 510-6194 ' ) ) "The secret compartment of my ring I fill / / _ , __o ____ with an Underdog super-energy pill." / (_