Friend declaration in C++ - difference between public and private

前端 未结 3 1989
花落未央
花落未央 2020-12-07 08:42

Is there a difference between declaring a friend function/class as private or public? I can\'t seem to find anything about this online.

I mean the difference between

相关标签:
3条回答
  • 2020-12-07 09:05

    The friend declaration appears in a class body and grants a function or another class access to private and protected members of the class where the friend declaration appears.

    As such access specifiers have no effect on the meaning of friend declarations (they can appear in private: or in public: sections, with no difference).

    0 讨论(0)
  • 2020-12-07 09:21

    Since the syntax friend class B doesn't declare a member of the class A, so it doesn't matter where you write it, class B is a friend of class A.

    Also, if you write friend class B in protected section of A, then it does NOT mean that B can access only protected and public members of A.

    Always remember that once B becomes a friend of A, it can access any member of A, no matter in which section you write friend class B.

    0 讨论(0)
  • 2020-12-07 09:27

    No, there's no difference - you just tell that class B is a friend of class A and now can access its private and protected members, that's all.

    0 讨论(0)
提交回复
热议问题