What is the difference between operator overloading using the friend
keyword and as a member function inside a class?
Also, what is the difference in the case of any unary operator overloading (i.e. as a friend vs. as a member function)?
A member function can be declared in private, public or protected scope of the class but have scope accordingly. While overloading binary operator, friend function takes two arguments. While overloading binary operator, member function takes one arguments.
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.
Most operators can be overloaded either as member or nonmember function. Some operators ( = [] () -> ) can be overloaded only as members. But you cannot have any oeprator both as a member and nonmember, specifically because it is unclear how to pick either one of them.
13. Which operator among the following can be overloaded using both friend function and member function? Explanation: Only the modulus operator among the given operators can be overloaded using either friend function or member function. Other operators must be overloaded using only the member functions.
Jacob is correct… a friend
function declared within a class has access to that class, but it's not inside the class at all, and everyone else has access to it.
For an operator overload which is not a member of the class (also called a free function, it may be a friend, or maybe not), the arguments are the same as the operands. For one which is a member of a class, the first operand is the "implicit argument" which becomes this
.
The implicit argument is different from the first argument to a free function in a few ways:
virtual
overload will be chosen by the dynamic type of the first operand, which is not possible with free functions without extra code.)The situation is the same for unary, binary, or n-ary (in the case of operator()
).
Members privilege of mutation: Operators which change the first operand (eg +=
, =
, prefix ++
) should be implemented as member functions, and should exclusively implement the guts of all overloads. Postfix ++
is a second-class citizen; it is implemented as Obj ret = *this; ++ this; return ret;
. Note that this sometimes extends to copy-constructors, which may contain *this = initializer
.
Rule of freedom for commuters: Only commutative operators (eg /
) should be free functions; all other operators (eg unary anything) should be members. Commutative operators inherently make a copy of the object; they are implemented as Obj ret = lhs; ret @= rhs; return ret;
where @
is the commutative operator and lhs
and rhs
are left-hand side and right-hand side arguments, respectively.
Golden rule of C++ friendship: Avoid friendship. friend
pollutes the semantics of a design. Overloading corollary: Overloading is simple if you follow the above rules, then friend
is harmless. friend
ing boilerplate overload definitions allows them to be placed inside the class {
braces.
Note that some operators cannot be free functions: =
, ->
, []
, and ()
, because the standard specifically says so in section 13.5. I think that's all… I thought unary &
and *
were too, but I was apparently wrong. They should always be overloaded as members, though, and only after careful thought!
The difference is that the friended function is actually in global scope, so you do not need to be an instance of the class to have access to it.
A member function requires that the left hand operator must be of that type. A friend function can allow implicit casting on the left hand operator.
So for example, lets say we create a BigInt class. And we create a member function operator + to take a right hand operator of BigInt.
Now lets also say BigInt has a constructor that takes a regular int. This constructor is NOT explicit (explicit keyword) and it takes one parameter. That means C++ can implitily convert from int to BigInt.
When we have these things we can do this:
BigInt foo( 5 ); BigInt bar; bar = foo + 5;
But we CAN'T do this:
BigInt foo( 5 ) BigInt bar; bar = 5 + foo;
However, if we used a friend function instead of a member function then both will work.
A member function requires that the left hand operator must be of that type. A friend function can allow implicit casting on the left hand operator.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With