Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!pacific.mps.ohio-state.edu!linac!att!ucbvax!lexvmc.vnet.ibm.com!schweitz From: schweitz@lexvmc.vnet.ibm.com ("Eric Schweitz") Newsgroups: comp.lang.c++ Subject: Re: Can a friend access members that were inherited as protected? Message-ID: <9104031422.AA01270@ucbvax.Berkeley.EDU> Date: 3 Apr 91 14:19:05 GMT Sender: daemon@ucbvax.BERKELEY.EDU Lines: 40 Cay Horstman writes: | class B | { | friend class PB; | }; | class PB | { | protected: | B* pb; | //... | }; | class PD2 : public PB | { | void print() { if (pb) cout << ((D*)pb)->d << " " << pb->b; } | }; | | why does the second example below, ... , not compile? PD2 is derived from PB in your example, so PD2 has access to all of the members of PB. PB is a friend of B so it has access to all the members of B. ``Friendship'' is not inherited, so PD2 does NOT have access to the members of B through PB. I hope this answers the question. Also note that in your example, PD2 can actually access members of B via the class D. PD2 is a friend of D which is derived (publicly) from B. So, if you wrote PD2::print() as: void PD2::print() { if (pb) ((D*) pb)->print(); } it should compile. (I'm assuming pb points to the B part of a D, as you showed.) Eric. -=- Standard Disclaimer Here -=-. Eric Schweitz Lexmark International