Path: utzoo!news-server.csri.toronto.edu!cs.utexas.edu!uunet!bywater!arnor!watson!blinn.watson.ibm.com!mittle From: mittle@blinn.watson.ibm.com (Josh Mittleman) Newsgroups: comp.lang.c++ Subject: Re: protected member functions Message-ID: <1991Mar15.165538.17532@watson.ibm.com> Date: 15 Mar 91 16:55:38 GMT References: <1991Mar14.215717.28199@asc.slb.com> Sender: @watson.ibm.com Reply-To: mittle@ibm.com Organization: IBM T. J. Watson Research Lines: 34 ARM, p.253, reads "A friend or member function of a derived class can access a protected nonstatic member of base of its base classes only through a pointer to, reference to, or object of the derived class (or any class derived from that class)." A discussion of this restriction can be found on pp.254-5. A::SetState() is a protected member of A and, by virtue of the public derivation, B::SetState() is a protected member of B. But they aren't the same thing, although they have the same implementation. Member functions of B can access B::SetState(), but they have no access to the protected parts of an A object; only members and friends of A can access those. Given your original code, suppose there were some other class C : public A, in which SetState() were overloaded: class C : public A { protected: void SetState(); }; Now, we do this: C objectC; B objectB(&C); // C is a A, so the C* is automatically converted to A*. B.Foo(); // wrong: invokes C->A::SetState() instead of // C->C::SetState(). Of course, this error could be avoided by making SetState() virtual, but it still is an incorrect result. =========================================================================== Josh Mittleman (mittle@ibm.com or joshua@paul.rutgers.edu) J2-C28 T.J. Watson Research Center, PO Box 704, Yorktown Heights, NY 10598