Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!rutgers!ucsd!sdcsvax!ucsdhub!calmasd!wlp From: wlp@calmasd.Prime.COM (Walter L. Peterson, Jr.) Newsgroups: comp.lang.c++ Subject: Re: Needed : Recommendations for C++ texts for beginner Message-ID: <282@calmasd.Prime.COM> Date: 14 Apr 89 15:45:22 GMT References: <1194@ncr-sd.SanDiego.NCR.COM> <1990009@hpctdls.HP.COM> <4009@brunix.UUCP> Organization: Prime-Calma, San Diego R&D, Object and Data Management Group Lines: 55 In article <4009@brunix.UUCP>, cs132046@brunix (Garrett Fitzgerald) writes: > In article <1990009@hpctdls.HP.COM> jeff@hpctdls.HP.COM (Jeff Hughes) writes: > > ... > > After reading the text, I typed in a couple of examples from chapter 6. > >These were the heterogenous linked list and search tree examples given > >in listings 6.5 - 6.7 and 6.10 - 6.11. That should get you off to a good > >start. > > Actually, I liked these examples too, and started planning a programming > assignment using the friend-type syntax that they used--and then got > hell from my TA for using friends. I was told that for real object-oriented > programming, I couldn't access the inner structure of the objects like that; > I could only write "object::set_x(int x1)" and "object::get_x" type routines > to look at them. > -------------------------------- I have to agree with your TA. There are seveal objections ( pun only slightly intended :-) ) to friends. From the "theoretical" standpoint, your TA is correct; that is not "real" object-oriented programming. Only the object itself, via methods defined on its class, should be able to access its fields. This is due to the simple fact that the implementation details of how the field data is handled is encapsulated in the object class definition. The details of how the class and its fields are implemented are not a matter for other methods and class to be concerned with. How does/can the other method "know" what type of data validation might be taking place within the access methods of the friend class ? It can't. It shouldn't. If you *REALLY* need to access a given field of an object in a particular function, then you should give some very serious thought to making that function a method on the class. There are also some very practical reasons for not using friends. What do you do if (I should say *WHEN*) the definition of the object class in question changes ? How many files do you have to search through to find the direct field accesses ? The object paradigm is supposed to, among other things, increase productivity by eliminating the need to do things like that whenever a change is made. As far as I'm concerned, the use of "friends" is a bad idea. The very existance of friends encourages "bad" habits. I think that it is analogous to allowing the use of (you should pardon the expression) "goto" in structured languages and should, like "goto", be avoided like the plague. With friends like that you don't need enemies. -- Walt Peterson. Prime - Calma San Diego R&D (Object and Data Management Group) "The opinions expressed here are my own and do not necessarily reflect those Prime, Calma nor anyone else. ...{ucbvax|decvax}!sdcsvax!calmasd!wlp