Path: utzoo!utgpu!news-server.csri.toronto.edu!rutgers!psuvax1!uwm.edu!cs.utexas.edu!ut-emx!slcs.slb.com!asc.slb.com!wkd From: wkd@asc.slb.com (Bill Duttweiler) Newsgroups: comp.lang.c++ Subject: protected member functions Message-ID: <1991Mar14.215717.28199@asc.slb.com> Date: 14 Mar 91 21:57:17 GMT Sender: news@asc.slb.com Organization: Schlumberger Austin Systems Center Lines: 38 Nntp-Posting-Host: ash Is it illegal to call a protected member function on an instance other than 'this'? Consider the following simple example: class A { public: A(); protected: void SetState(); }; class B : public A { public: B(A* a) { MyA = a; } void Foo() { MyA->SetState(); } protected: A *MyA; }; When run through g++ (1.37.1), I get the following error message: test.cc: In method void B::Foo (): test.cc:13: method `void A::SetState ()' is protected When run through Sun C++ (based on cfront 2.0), I get "test.cc", line 13: error: B::Foo() cannot access A::SetState(): protected member So, these compilers agree. But I don't: B is publicly derived from A and should have access to protected member functions from within its own member functions. I can call SetState() on 'this', why not on MyA??? -- William K. Duttweiler Schlumberger Austin Systems Center (512) 331-3000 PO Box 200015 wkd@asc.slb.com Austin, TX 78720-0015