Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!usc!ucselx!petunia!kestrel.edu!gyro From: gyro@kestrel.edu (Scott Layson) Newsgroups: comp.std.c++ Subject: Re: protected != public-to-derived Message-ID: <1991Mar24.010442.27298@kestrel.edu> Date: 24 Mar 91 01:04:42 GMT References: <1991Mar19.165333.23908@asc.slb.com> Sender: Gyro@Reasoning.COM Organization: Kestrel Institute, Palo Alto, CA Lines: 55 In article <1991Mar19.165333.23908@asc.slb.com> wkd@asc.slb.com (Bill Duttweiler) writes: > >I posted this to comp.lang.c++ a week ago, and haven't gotten any >feedback. Since I neglected to post to comp.std.c++, I thought I'd try >THAT before giving up on it... > >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: > [...] The second sentence of section 11.5 of E&S (p. 253) reads: A friend or a a member function of a derived class can access a protected nonstatic member of one of its base classes only through a pointer to, reference to, or obejct of the derived class (or any class derived from that class). I have bumped my head on this rule a couple of times, and frankly don't agree with the rationale given in E&S for its existence (see the commentary starting on the bottom of p.254). The last sentence of that rationale, which describes a bug someone once had which the rule would have prevented, reads: Naturally, the use of a virtual function for performing the update would have avoided the problem. Seems to me that this sentence admits that the bug really had quite another cause than the mistaken access of a protected member. Since the issue has come up, I am very much interested to hear the committee's current feeling about this rule. Unless that feeling turns out to be one of utter attachment, I may consider submitting a formal proposal and argument that it be rescinded (which would be a compatible change, affecting no previously legal code). -- Scott Layson Burson