Where would you use a friend function vs. a static member function?

后端 未结 15 2204
[愿得一人]
[愿得一人] 2020-12-02 04:56

We make a non-member function a friend of a class when we want it to access that class\'s private members. This gives it the same access rights as a static member function w

15条回答
  •  失恋的感觉
    2020-12-02 05:20

    Section 11.5 "The C++ Programming Language" by Bjarne Stroustrup states that ordinary member functions get 3 things:

    1. access to internals of class
    2. are in the scope of the class
    3. must be invoked on an instance

    friends get only 1.

    static functions get 1 and 2.

提交回复
热议问题