I have a class (class A) that is designed to be inherited by other classes written by other people. I also have another class (class B), that also inherits from A.
B has to access some A’s member functions that shouldn’t be accessed by other inheriting classes.
So, these A’s member functions should be public for B, but private for others.
How can I solve it without using ‘friend’ directive?
Thank you.
EDIT: Example why I need it.
class A { public: void PublicFunc() { PrivateFunc(); // and other code } private: virtual void PrivateFunc(); }; class B : public class A { private: virtual void PrivateFunc() { //do something and call A's PrivateFunc A::PrivateFunc(); // Can't, it's private! } };
What you say is: there are two sets of subclasses of A. One set should have access, the other set shouldn’t. It feels wrong to have only one brand of subclasses (i.e. B) ‘see’ A’s members.
If what you mean is: only we can use this part of functionality, while our clients can’t, there are other resorts.
(Functionality reuse by inheritance often corners you with this kind of problems. If you go towards reuse by aggregation, you may get around it.)
A suggestion:
But better would be to go the aggregation way, and split the current ‘A’ into a visible and an invisible part: