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

后端 未结 15 2164
[愿得一人]
[愿得一人] 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:27
    1. Static data members always share the memory.
    2. only static function can used static data members.
    3. static member function can be called with class name.
    4. They must be defined outside of the class when we create a object of static member or member function in the class. It will automatically initialize the value.
    5. It always used keyword static.
    6. Static members can share by all the objects.
    7. Type and scope of data members and member function is outside of the class.
    8. A static member variable must be defined outside of the class.
    0 讨论(0)
  • 2020-12-02 05:28

    Friend functions can access the private and protected members of other classes. Means they can be used to access all the data weather it is private or public. So friend functions are used to access that data which static methods can not.

    Those methods are made static which are called so many times that declaring a different location inside every object, for them becomes too costly(In terms of memory). This can be made clear with the help of example: Let the class's name is fact and its data member is n(which represents integer whose factorial is concern) then in this case declaring find_factorial() as static would be wise decision!!

    They are used as callback functions to manipulate class-scoped members to retrieve constant data that you don't want to enumerate in your header file

    Now we are clear with following questions..

    When a friend function is used? When a static function is used?

    Now If both are viable options to solve a problem, We can weight up their suitability in terms of accessibility(accessibility of Private data) and memory efficiency. By default no one can be preferred as there are many situation when we need better memory management and sometimes we are are concerned with the scope of data.

    For example: foo::create() will be preferred over create_foo() when we have to call create() method after every small instance of time and we are not interested on scope of data(Private data)

    And if we are interested to get the private information of more than one class(s) then create_foo() will be preferred over foo::create().

    I hope this would help you!!

    0 讨论(0)
  • 2020-12-02 05:30

    Static function can only access members of one class. Friend function has access to several classes, as explained by the following code:

    class B;
    class A { int a; friend void f(A &a, B &b); };
    class B { int b; friend void f(A &a, B &b); };
    void f(A &a, B &b) { std::cout << a.a << b.b; }
    

    f() can access data of both A and B class.

    0 讨论(0)
  • 2020-12-02 05:31

    A friend function can not be inherited while a static function can be. So when an aim can be achieved with both static function and friend function, think that whether you want to inherit it or not.

    0 讨论(0)
  • 2020-12-02 05:32

    You would use a static function if the function has no need to read or modify the state of a specific instance of the class (meaning you don't need to modify the object in memory), or if you need to use a function pointer to a member function of a class. In this second instance, if you need to modify the state of the resident object, you would need to pass this in and use the local copy. In the first instance, such a situation may happen where the logic to perform a certain task is not reliant on an object's state, yet your logical grouping and encapsulation would have it be a member of a specific class.

    You use a friend function or class when you have created code that is not a member of your class and should not be a member of your class, yet has a legitimate purpose for circumventing the private/protected encapsulation mechanisms. One purpose of this may be that you have two classes that have need of some common data yet to code the logic twice would be bad. Really, I have only used this functionality in maybe 1% of the classes I've ever coded. It is rarely needed.

    0 讨论(0)
  • 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.

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