Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

What is friend function in operator overloading?


Asked by Mohammad Bernard on Dec 09, 2021 FAQ



Friend function using operator overloading offers better flexibility to the class. These functions are not a members of the class and they do not have 'this' pointer. When you overload a unary operator you have to pass one argument. When you overload a binary operator you have to pass two arguments.
Just so,
When an operator overloaded function is a friend function, it takes two operands of user-defined data type. When redefining the meaning of an operator by operator overloading friend function, we cannot change its basic meaning. For example, we cannot redefine minus operator - to divide two operands of user-defined data-type.
Indeed, C++ allows you to specify more than one definition for a function name or an operator in the same scope, which is called function overloading and operator overloading respectively.
In addition,
Overloading Binary Operator using a Friend function: In this approach, the operator overloading function must precede with friend keyword, and declare a function class scope. Keeping in mind, friend operator function takes two parameters in a binary operator, varies one parameter in a unary operator.
In fact,
Operator cannot be used to overload when declaring that function as friend function = () [] ->. Overloading Unary Operator: Let us consider to overload (-) unary operator. In unary operator function, no arguments should be passed. It works only with one class objects. It is a overloading of an operator operating on a single operand.