Article View: comp.std.c++
Article #2937Re: Encapsulation and friends
From: Steve Clamage
Date: Thu, 14 Dec 1995 20:59
Date: Thu, 14 Dec 1995 20:59
41 lines
2722 bytes
2722 bytes
In article eos@mhost.ida.his.se, anders@ida.his.se (Anders Eklund) writes: > >Friends break the rules of encapsulation and should be avoided >as much as possible, but sometimes they must be used anyway. >When I have used friends they only had to use one or a >few members of the class. As it is now all members are >accessible to the friend. I would like to specify which members >the friend can access. There is no way to do that today, but >would it not be nice to be able to do so? Any comments? I disagree with your premise. Friends can be used to violate encapuslation, but so can the "public:" access specifier. A friend function does not break encapsulation merely by existing. A friend function must be declared with the class. The owner of the class also owns the friend functions. IMHO you should view a friend as part of the public interface. That is, from a class design point of view, a friend function is equivalent to a public static member function: it has no "this" pointer, but can access the entire class. The only significant difference is the syntax used to access the function. For example, an overloaded operator+() as a static member function makes little sense; you could not use the infix operator notation. As a non-static member function it is asymetric and thus inconvenient to use. As a friend function it is convenient to use with no surprises. If it makes sense to provide a way to limit the access a friend function has to a class, it must also make sense to limit the access of a member function, yet nobody ever suggests the latter as a good idea. It seems to me that fine-grained access control (e.g. function A can access members x, y, and z; function B can access member x only; function C can access y and z) would be cumbersome to use and would make maintenance more difficult. I don't see that it has any advantages. --- Steve Clamage, stephen.clamage@eng.sun.com [ comp.std.c++ is moderated. Submission address: std-c++@ncar.ucar.edu. Contact address: std-c++-request@ncar.ucar.edu. The moderation policy is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
Technical Details
Message-ID:
<4aq2mk$9me@engnews1.eng.sun.com>Path:
arc1-ca.pugleaf.net!engnews1.Eng.Sun.COM!taumet!clamageReferences:
<4apdff$eos@mhost.ida.his.se>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!engnews1.Eng.Sun.COM!taumet!clamage From: clamage@Eng.sun.com (Steve Clamage) Newsgroups: comp.std.c++ Subject: Re: Encapsulation and friends Date: 14 Dec 1995 20:59:21 GMT Organization: Sun Microsystems Inc. Approved: clamage@eng.sun.com (comp.std.c++) Message-ID: <4aq2mk$9me@engnews1.eng.sun.com> References: <4apdff$eos@mhost.ida.his.se> Reply-To: clamage@Eng.sun.com NNTP-Posting-Host: taumet.eng.sun.com Content-Type: text X-Nntp-Posting-Host: taumet.eng.sun.com Content-Length: 1912 X-Lines: 39 Originator: clamage@taumet