🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c++
Article #3692

Re: Anyone considered inheritable friendship?

#3692
From: Russell Gold
Date: Sat, 16 Mar 1996 07:44
73 lines
3301 bytes
Eugene Radchenko wrote:

[ Steve Clamage comments that making inheritance inheritable is
  roughly equivalent to making all members public, in terms of
  its effects on maintenance ]

> But it is not. Inheritable friendship is a natural extension of
> normal friendship (for tightly coupled classes) to tightly
> coupled hierarchies.

Except that hierarchies are necessarily open, so making a 
hierarchy a friend means that the original class becomes open to 
classes which were never imagined by its author.  You can never 
guarantee that a "tightly coupled hierarchy" will remain tightly 
coupled.  AFAIK there is no prohibition on making multiple members 
of the same hierarchy friends to the same class.  Doing this allows 
the original author to restrict access to only those members of the 
hierarchy which should have that access.

> In my project (that spawned the question) base class builds a
> certain graph and derived classes decompose it in different ways 
> using helper objects
> from other hierarchies. Nobody except those objects (and original
> object tree, of course) has any business to that graph.

So make each of those classes friends of the graph.

OR, you can use a little trick within the confines of the language.

class ThatGivesFriendship
    :
    
  friend HierarchyBaseClass;
  private:    // operations used by the hierarchy
    short accessedInfo() const;
    :    

  private:    // members only used by the class itself
    :

  };


class HierarchyBaseClass
   :
   :

  protected:
    short tgfAccessedInfo( const ThatGivesFriendship *tgf ) const {
          return tgf-> accessedInfo();
          }

   :

In this scheme, every subclass in the hierarchy *does* have access 
to those members of the relevant class without making friendship 
inheritable.  Of course, here, you are specifically identifying the 
interface between the classes, and stating that only members of the 
hierarchy can use it.

And you can do all of this without modifying the language.

-- 
Russell Gold         | (609) 727-2219    
ADP, Inc.            | goldr@bis.adp.com
201 E. Park Dr.      | russgold@ACM.org                  
Mt. Laurel, NJ 08054 | russgold@aol.com
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]
Technical Details
Message-ID: <3149AC2A.42D2@bis.adp.com>
Path: arc1-ca.pugleaf.net!in1.uu.net!bounce-back
References: <4fnrfr$7b1@engnews1.Eng.Sun.COM> <AFwV3Hn027@qsar.chem.msu.su>

Show Full Headers
Complete Article Headers
From: Russell Gold <goldr@inet.bis.adp.com>
Message-ID: <3149AC2A.42D2@bis.adp.com>
X-Original-Date: Fri, 15 Mar 1996 12:43:06 -0500
Path: arc1-ca.pugleaf.net!in1.uu.net!bounce-back
Date: 16 Mar 96 07:44:23 GMT
Approved: fjh@cs.mu.oz.au
Return-Path: <daemon@migs.UCAR.EDU>
Newsgroups: comp.std.c++
Subject: Re: Anyone considered inheritable friendship?
Organization: Strategic Architecture
References: <4fnrfr$7b1@engnews1.Eng.Sun.COM> <AFwV3Hn027@qsar.chem.msu.su>
X-Mailer: Mozilla 2.0 (WinNT; I)
X-Auth: PGPMoose V1.1 PGP comp.std.c++
 iQBFAgUBMUpxYeEDnX0m9pzZAQH2CgF+ISS/R4gAfc74Pxr9r/3703GSEnlvkYcM
 MnNOiYBpbY29uLXaA3czBEW+5jtqNel2
 =eE4W