friend class with limited access

后端 未结 3 1877
轮回少年
轮回少年 2020-11-30 02:41

I want to make a class A friend class of class B. I want to do this as these interact very much and A needs to change internals of class B (which I dont want to expose using

3条回答
  •  自闭症患者
    2020-11-30 03:20

    One approach is through explicit interfaces, because the implementor of an interface can select who they give them to:

    class NearlyPrivateInterface {
    public:
       virtual void setState() = 0;
       virtual void setFlags() = 0;
    };
    
    class A {
    public:
       void attach(NearlyPrivateInterface* instanceOfB);
    };
    
    class B: private NearlyPrivateInterface {
    public:
       void attach(A& a) { a.attach(this); }
    };
    

提交回复
热议问题