Path: utzoo!utgpu!water!watmath!clyde!att-cb!osu-cis!tut.cis.ohio-state.edu!rutgers!bellcore!faline!thumper!ulysses!andante!alice!shopiro From: shopiro@alice.UUCP Newsgroups: comp.lang.c++ Subject: Re: Critique of Wiener/Pinson Summary: arguments for access functions Message-ID: <7814@alice.UUCP> Date: 17 Apr 88 02:26:28 GMT References: <8063@apple.Apple.Com> <6590037@hplsla.HP.COM> Organization: AT&T Bell Laboratories, Murray Hill NJ Lines: 51 Posted: Sat Apr 16 22:26:28 1988 In article <6590037@hplsla.HP.COM>, jima@hplsla.UUCP writes: > I don't buy the idea that instance variables of an object must always > be accessed via an access method. > To suggest that: > > x = myComplexNumber.re(); > y = myComplexNumber.im(); > > is in any way better than: > > x = myComplexNumber.re; > y = myComplexNumber.im; > > seems ludicrous to me. Here are two reasons you might prefer access functions. 1) Access functions allow you to change the internal representation. You might someday want to represent complex numbers in polar coordinates. Admittedly, rectangular coordinates are usually preferred, but there undoubtedly are applications in which polar coordinates would be better. Use access functions and all your code will continue to run. 2) Access functions support read-only access to instance variables. You might someday want a redundant represention of complex numbers using both rectangular and polar coordinates. You then need to preserve the consistency of the two parts of the representation. If you allowed the user direct access to the instance variables, that consistency could be destroyed by any code that accessed a complex number. If you use access functions, you only have to worry about member functions. I should also mention that there is no performance penalty for access functions. If they are expanded inline the generated code is exactly the same as reading a public instance variable. On the other hand C++ does allow public instance variables, so you can make your own decision. > In fact, I believe that in many situations similar to > the complex number example, allowing direct access is preferrable to forcing > the class's user to always tag on the "meaningless" () parenthesis. I will agree that the required () for access functions may seem annoying, but if you agree with me that all syntax is painful, then I hope you'll also agree that this is only a minor turn of the screw. (:-) -- Jonathan Shopiro AT&T Bell Laboratories, Murray Hill, NJ 07974 research!shopiro (201) 582-4179