So my Idea is simple – to have a class that can have its own public methosds and a nested class that would forvard only some of that public methods (pseudocode):
class API
{
public:
go();
stop();
friend class B
{
public:
void public_request()
{
request();
}
void public_go()
{
go()
}
};
private:
void request(){}
};
Is it possible to implement such nested class in C++ and how?
Yes, but you have to provide an instance of the outer class when you create the inner one… the two types are [mostly] independant, so
Bis not associated with any specific instance ofAPI.More pseudo-code
Alternatively, you can pass an instance of
APItoB‘s function [B::go(API&)]Further,
APIdoes not contain an instance ofBunless you explicitely add an instance.Also, note that
Bdoes not need to be granted friendship byAPI. As an inner class,Bcan already accessAPIs private/protected members. The converse is not true, however…APIcan not accessBs private/protected members unlessBgrants it permission.