Don't you think friend function makes private member vulnerable towards data security? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Don't you think friend function makes private member vulnerable towards data security?

As friend function can access private members of a class I think data security is compromised somewhere. What is the use of friend function?

2nd Nov 2017, 1:59 AM
Ankit Kumain
Ankit Kumain - avatar
1 Answer
+ 1
It does, that's one reason why many people don't recommend using it. Also, if your friend function accesses private members, that function will depend on the implementation of your class. This means that if you change the implementation of the class, you may have to change the friend function as well. It's recommended to use getters and setters instead of exposing private variables to friend functions. This way other parts of the code will only see the interface and the underlying implementation can change without having to worry about it.
2nd Nov 2017, 4:09 AM
OWRhcmFidQ==