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

后端 未结 15 2248
[愿得一人]
[愿得一人] 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:33

    Static functions are used when you want a function that is the same for every instance of a class. Such functions do not have access to "this" pointer and thus cannot access any non static fields. They are used often when you want a function that can be used without instantiating the class.

    Friend functions are functions which are not in the class and you want to give them access to private members of your class.

    And this(static vs. friend) is not a matter of using one vs the other since they are not opposites.

提交回复
热议问题