Click here to Skip to main content
15,912,400 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Is there one reason or another to choose a friend function for an operator vs just making it a member of the class? It seems lots of people use the friend function for operators but im not sure what the reasoning is. Is it better to do it one way or the other? How do you know which one to use?

What I have tried:

I have been reading about friend and operators. Both are new to me in general but I have been trying the friend operator examples.
Posted
Updated 17-Nov-22 20:28pm

Here is some documentation on the topic : cplusplus.com : inheritance[^]

It has to do with access control because friend classes and functions have access to non-public methods and members of a class.
 
Share this answer
 
Suppose you define a new type, e.g. Person. Then you want to provide the insertion operator (<<) in order to output the Person's details.
Does it make sense to make such operator a member of Person class? Does it make sense to make it a member of the ostream class? Possibly it does make more sense to implement it as a friend at global scope.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900