Path: utzoo!attcan!uunet!mcvax!ukc!reading!cf-cm!cybaswan!eeartym From: eeartym@cybaswan.UUCP (Dr R.Artym eleceng ) Newsgroups: comp.lang.c++ Subject: Re: Friend specifier considered harmful Message-ID: <80@cybaswan.UUCP> Date: 12 Sep 88 02:52:26 GMT References: <61@cybaswan.UUCP> <1988Sep2.174327.6439@ateng.uucp> <5870@columbia.edu> Reply-To: eeartym@cybaswan.UUCP (Dr R.Artym eleceng ) Organization: University College of Swansea Lines: 37 In article <5870@columbia.edu> fox@garfield (David Fox) writes: > In article <1988Sep2.174327.6439@ateng.uucp> chip@ateng.UUCP (Chip Salzenberg) > writes: > > >I used "friend" once where it proved very useful: an implementation of a > >hash table. > > > >The HashTable class contains an array of pointers to HashNode. Each > >HashNode contains a pointer to the next HashNode. I made HashTable a friend > >of HashNode. This allowed easy access to the HashNode "next" pointer but > >_only_ for HashTable member functions. > > Yes, this is an example of `friend' creating *more* type security rather than > less. The HashNode class need have *no* public members whatsoever, so that > the class itself effectively becomes a private `member' of the HashTable > class. Friend classes without public members may look appealing at first sight, but in a non-trivial application the lack of data hiding amongst the friends would be a far greater source of concern than the possibility of non-local use of a class resulting from the presence of public members. After all, the latter misuse is easily detectable and can't impinge on the proper operation of the rest of the code (as long as static data is not used). The HashTable application doesn't warrant the use of friends at all, in my opinion, as it can be programmed easily and efficiently with full isolation between classes. -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Keywords: Parallel, Applicative, and Object-Oriented Languages and Systems --------------------------------------------------------------------------- Dr. Richard Artym, + UUCP : ..!ukc!pyr.swan.ac.uk!eeartym Electrical Engineering Dept., + JANET : eeartym@uk.ac.swan.pyr University of Wales, + Phone : [(0792) or (+44 792)] 295536 Swansea, SA2 8PP, + Fax : [(0792) or (+44 792)] 295532 U.K. + Telex : 48358 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~