+ 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.

24th Jul 2016, 6:16 PM
Aditya Pai
Aditya Pai - avatar
1 Odpowiedź
+ 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.
24th Jul 2016, 11:30 PM
Stefan
Stefan - avatar