Allowing a “friend” class to access only some private members

前端 未结 7 1838
迷失自我
迷失自我 2020-12-13 12:54

Suppose I have three C++ classes FooA, FooB and FooC.

FooA has an member function named Hello, I want to call this function in class FooB, but I don\'t

7条回答
  •  情书的邮戳
    2020-12-13 13:51

    You'll need inheritance. Try this:

    // _ClassA.h
    class _ClassA
    {
      friend class ClassA;
    private:
      //all your private methods here, accessible only from ClassA and _ClassA.
    }
    
    // ClassA.h
    class ClassA: _ClassA
    {
      friend class ClassB;
    private:
      //all_your_methods
    }
    

    This way you have: ClassB is the only one to be able to use ClassA. ClassB cannot access _ClassA methods, that are private.

提交回复
热议问题