From kde-devel Thu Apr 04 18:34:00 2002 From: Patrick Julien Date: Thu, 04 Apr 2002 18:34:00 +0000 To: kde-devel Subject: Re: accessing protected members of a parent class X-MARC-Message: https://marc.info/?l=kde-devel&m=101794522914352 Ellis Whitehead wrote: > On Thursday 04 April 2002 11:40, Patrick Julien wrote: > >>you have to make B a friend in the protected or private section of class A. > > > That is in fact what I've been doing to get around the problem. But its > ugliness quotient is horrendous and you can only use this approach for > sub-classes which you program yourself -- leaving the option unavailable to > programmers using the library... I agree:). How about ... class A { protected: void access_link(A* a) {a->link();}//Access_link is not virtual virtual void link(A* a) {/*...*/} }; class B : public A { protected: virtual void link(A* a) { access_link(a); /*...*/ } }; Now, if you really want to get creative (or ugly depending on how you see it:) have access_link return a pointer to the link method, then you can use it from B::link(). > > > >>Ellis Whitehead wrote: >> >>>Is it possible to access protected members in an object whose type is a >>>superclass of the current method's class? Somehow in over ten years of >>>C++ programming, I've missed or forgotten the fact that this isn't >>>allowed. :( >>> >>>Compiling the following code, for example, results in the following >>>error: >>> >>>$ g++ test.cpp -o test.o >>>test.cpp: In method `void B::link(A *)': >>>test.cpp:4: `void A::link(A *)' is protected >>>test.cpp:15: within this context >>> >>> >>>class A >>>{ >>> protected: >>> virtual void link( A* ); >>>}; >>> >>>class B : public A >>>{ >>> protected: >>> virtual void link( A* ); >>>}; >>> >>>void B::link( A* a ) >>>{ >>> a->link( 0 ); // *** fails *** >>>} >>> >>> >>>>>Visit http://mail.kde.org/mailman/listinfo/kde-devel#unsub to >>>>>unsubscribe << >>>> > > > >>>Visit http://mail.kde.org/mailman/listinfo/kde-devel#unsub to unsubscribe << >> -- Patrick Julien Ideas & Associates (514) 940-2891x211 >> Visit http://mail.kde.org/mailman/listinfo/kde-devel#unsub to unsubscribe <<