0
friend func param obj ptr vs obj ref
func(Class *obj) or func(Class &obj) which is better, in which case? thx
3 odpowiedzi
+ 4
As you know both will work.
References are more clear to use as compared to pointers.
If you use pointer then while accessing data members and member functions you'll have to use -> member access operator but when you use references it just creates alias for same object/variable so it behaves as normal object and therefore you can access it's properties just by using period (.) /dot operator
Ex.
Using pointer.
objptr->method(); OR
(*objptr).method();
Using reference.
objrfr.method();
Now there is no rule which one you should use. You can use any one you like but references are more common to be used in case of friend function.
though it's not part of your question it worth noting that if you don't have to modify values of properties of object you'll not need any of these(pointer,reference).
ex. friend function that displays contents of object and don't modify anything can be called by value //no need of ptr or ref
+ 1
BinaryEden
If you don't want to use any properties of object then it's not necessary to pass object to friend.
Suppose you got a friend function that prints "Hello world" then it doesn't need to have any object as parameter.
But in case you have to access private /public/protected data of any object then you must pass it to friend function.
yes as you stated it'll be useless to have such a friend function that can't access data members of object.
And please note that you can mention me or others by using @ symbol.
When you mention a person they get notification about it .Otherwise we can't notice your replies to this thread.
Thank you .
0
so a friend func without obj parameters is useless then