Path: utzoo!utgpu!news-server.csri.toronto.edu!mailrus!iuvax!cica!tut.cis.ohio-state.edu!ucsd!ucsdhub!hp-sdd!apollo!williams_j From: williams_j@apollo.HP.COM (Jim Williams) Newsgroups: comp.lang.c++ Subject: Re: Derived can't access protected members of base's Message-ID: <49548fe5.20b6d@apollo.HP.COM> Date: 21 Mar 90 18:10:00 GMT References: <56.UUL1.3#5109@pantor.UUCP> <10593@alice.UUCP> Sender: root@apollo.HP.COM Lines: 32 Andrew Koenig writes (6443): > 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. Couldn't you also write: class FooPub { public: int i; }; void clobber (Foo& f, int n) { ((FooPub*)&f)->i = n; } Why have private/protected members at all? :-)