Path: utzoo!utgpu!news-server.csri.toronto.edu!mailrus!cs.utexas.edu!tut.cis.ohio-state.edu!att!dptg!ulysses!andante!alice!ark From: ark@alice.UUCP (Andrew Koenig) Newsgroups: comp.lang.c++ Subject: Re: Derived can't access protected members of base's Message-ID: <10593@alice.UUCP> Date: 18 Mar 90 14:16:35 GMT References: <56.UUL1.3#5109@pantor.UUCP> Organization: AT&T Bell Laboratories, Liberty Corner NJ Lines: 34 In article <56.UUL1.3#5109@pantor.UUCP>, richard@pantor.UUCP (Richard Sargent) writes: > To our mind, we have a class derivation tree (root class and a number > of derived branches). We think that any object of a type from that > tree of classes should be able to access the protected members of > other objects also of that tree, but no class not in the tree. 1.2 enforced the restriction too, and I don't think the enforcement changed significantly between 1.2 and 2.0. One of the main reasons for the restriction is that otherwise there's very little practical difference between `protected' and `public.' For example: class Foo { protected: int i; }; Now, suppose I want to clobber member `i' of a `foo' object. I would like to write this: void clobber(Foo& f, int n) { f.i = n; } but I can't quite do that directly. What if I do this, though? class dummy: public Foo { friend void clobber(Foo&, int); }; Now `dummy' can get at any protected member of Foo, and so can its friends. By making `clobber' a friend, I open Foo up to it, and completely without permission from the author of Foo. -- --Andrew Koenig ark@europa.att.com