What is the use of friend function in operator overloading in C++?
Sophia Koch
Updated on June 01, 2026
.
Thereof, what is the use of friend function in operator overloading in C++?
Friend function generally helps you to access private data members of a class which is contradictory to encapsulation. In operator overloading, if an operator is overloaded as member, then it must be a member of the object on left side of the operator.
Also Know, what is the difference between overloading a operator using friend function and using member function? Overloading with friend Function. The friend functions are more useful in operator overloading. The difference between member function and friend function is that the member function takes argument explicitly. On the contrary, the friend function needs the parameters to be explicitly passed.
Consequently, which operator can be overloaded in C++ using friend function?
Answer: Friend function is a function that can access the data from private, protected and public class. If these operators are overloaded using friend function, then program will result with compilation error. These operators can be overloaded if they are part of member functions.
What is operator overloading with example?
Operator overloading allows you to redefine the way operator works for user-defined types only (objects, structures). It cannot be used for built-in types (int, float, char etc.). Two operators = and & are already overloaded by default in C++. For example: To copy objects of same class, you can directly use = operator.
Related Question Answers