When should you use 'friend' in C++?

前端 未结 30 2184
孤街浪徒
孤街浪徒 2020-11-22 10:12

I have been reading through the C++ FAQ and was curious about the friend declaration. I personally have never used it, however I am interested in exploring the language.

30条回答
  •  半阙折子戏
    2020-11-22 10:37

    @roo: Encapsulation is not broken here because the class itself dictates who can access its private members. Encapsulation would only be broken if this could be caused from outside the class, e.g. if your operator << would proclaim “I'm a friend of class foo.”

    friend replaces use of public, not use of private!

    Actually, the C++ FAQ answers this already.

提交回复
热议问题