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

前端 未结 7 1833
迷失自我
迷失自我 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:37

    There's nothing to make a class a friend of one specific function, but you can make FooB a friend of a "key" class with private constructor, and then have FooA::Hello take that class as an ignored parameter. FooC will be unable to provide the parameter and hence can't call Hello:

    Is this key-oriented access-protection pattern a known idiom?

提交回复
热议问题