Path: utzoo!attcan!uunet!lll-winken!lll-tis!ames!mailrus!tut.cis.ohio-state.edu!rutgers!bellcore!faline!thumper!ulysses!andante!alice!shopiro From: shopiro@alice.UUCP (Jonathan Shopiro) Newsgroups: comp.lang.c++ Subject: Re: Friend specifier considered harmful Summary: friend status can be granted to individual class members Message-ID: <8180@alice.UUCP> Date: 9 Sep 88 15:35:44 GMT References: <61@cybaswan.UUCP> <8160@alice.UUCP> <29433@bbn.COM> Organization: AT&T Bell Laboratories, Murray Hill NJ Lines: 34 In article <29433@bbn.COM>, lpringle@bbn.com (Lewis Pringle) writes: > ... > It seems to me that the trouble is that C++ provides a restrictive form of > data hiding (not that I know of a better way!): its too "all or nothing". > The use of friends is not exacly what you want. Perhaps if in > specifying a friend,you didn't give blanket access to the given class, > but instead to just an explicitly stated set of members? Good idea! In fact you can do exactly that, for example class Foo { int bar(); int baz(); }; class Foe { friend int Foo::bar(); int priv; }; Then Foo:bar() can access priv in any Foe object, but Foo::baz() cannot. I should also say that there seems no limit to the gradations of data hiding that a programmer might want, for example you might want one private member to be accessible to one external function and another private member to be accessible only to a different external function. However, the fundamental purpose of data hiding in C++ is to help the programmer avoid bugs and it has no impact whatsoever on correct programs. Therefore, more elaborate data hiding schemes have two strikes against themselves at the outset: they make things more complex by their very existence, but their justification must be that they make things simpler. -- Jonathan Shopiro AT&T Bell Laboratories, Murray Hill, NJ 07974 research!shopiro (201) 582-4179