Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!usc!apple!netcom!sjsumcs!horstman From: horstman@mathcs.sjsu.edu (Cay Horstmann) Newsgroups: comp.lang.c++ Subject: Can a friend access members that were inherited as protected? Message-ID: <1991Mar31.205839.16204@mathcs.sjsu.edu> Date: 31 Mar 91 20:58:39 GMT Organization: San Jose State University - Math/CS Dept. Lines: 70 This is for language lawyers... Below you will find a base class B with a protected member b, a derived class D which declares classes PD, PD1 as friends. With Borland C++ (2.0), PD member functions can access b. I think this is as it should be. As I read it, declaring a class PD as a friend of class D means that all member functions of class PD are friends of D and friend functions of D have the same access rights as member functions of D. Since member functions of D can access protected members of B, so should friend functions of D. Is this interpretation correct? Does it reflect the reality in 2.0 compilers? If so, why does the second example below, with the class PD1, not compile? If this is a Borland bug, I guess I ought to mail it to them. Can anyone remember the recently posted internet address for this purpose? Thanx, Cay ------------------------------------------------------------------------------ #include class B { protected: int b; friend class PB; public: B( int n ) { b = n; } void print() { cout << b; } }; class D : public B { int d; friend class PD; friend class PD2; public: D( int m, int n ) : d( m ), B( n ) {} void print() { cout << d << " " << b; } }; class PD { D* pd; public: PD( D* p ) : pd( p ) {} void print() { if( pd ) cout << pd->d << " " << pd->b ; } }; // ok, this shows that the friend PD of D can access the protected inherited b class PB { protected: B* pb; public: PB( B* p ) : pb( p ) {} void print() { if( pb ) cout << pb->b ; } }; class PD2 : public PB { PD2( D* p ) : PB( p ) {} void print() { if( pb ) cout << ((D*)pb)->d << " " << pb->b ; } }; // Error message: B::b is not accessible in function PD2::print() // Why can the friend PD2 of D can not access the protected inherited b ?