+ 4
While prototyping friend functions in a class is it necessary to always pass the address of the object as parameter?If so why is it that we have to pass address as parameter..In programs involving compositions we never used addresses...Why is the need for address of the objects while using friend?
can it be done without passing the address of the object? please explain why passing address is necessary to change the value of the variable.
1 Answer
+ 9
Hi Wayne! Yes you always have to pass the object itself. This is because using friend functions, a technical detail becomes visible that is hidden by the C++ compilers.
If you declare a function "friend" in a class, it leaves the namespace of the class in which it is declared as friend.
This requires you to state an instance to the class as first parameter for the function.
About the technical detail that is hidden by the C++ compilers: each function of a class is actually just a free function outside the class with a class instance as the first parameter and full access privileges to the fields and other functions.
Therefore a friend function is not that much different to a "normal" class function, it just looks different as a hidden detail becomes visible.