Difference between Static and Friend Function in C++
Static Function: It is basically a member function that can be called even when the object of the class is not initialized. These functions are associated with any object and are used to maintain a single copy of the class member function across different objects of the class. This function is denoted by using the static keyword.
Friend Function: It is basically a function that is especially required for accessing non-public members of the class. It has the right to access all private and protected members of the class. It usually provides some additional functionality that is not normally used by class and allows sharing provide class information by non-member function.
Tabular difference between Static Function and Friend Function: Static Function Friend Function
It is a member function of a class that is called even when the object of the class is not initialized. It is a function that is declared outside the class scope. In this, it cannot access any variable of its class except for static variables. In this, it can access private and public members of the class. It is denoted by placing a static keyword before the function name. It is denoted by placing a friend keyword before the function name. This function is generally used to make function members independent of any particular object of the class. This function is generally used to access non-public members of the class. These functions are normally used when one wants a function that is the same for every instance of the class. These functions are normally used to share information of class that was hidden previously. It can have access to members of one class. It can have access to members of several classes. It cannot be used when one needs to overload operators. It can be used when one needs to overload operations because overloading operators can only be done through friends or non-static members. It can also be used if the function has no need to read, change or modify the state of a particular instance of the class or if one needs to use function pointer to a member function of class. It can also be used when one wants to create code that not a member of the class and should not be a member of their class. This function can be hidden behind privileges. This function cannot be hidden and anyone may call the friend function. It is associated with class and not an object. It is declared in class but does not belong to the class.