C++ Friend function | Sololearn: Learn to code for FREE!

+4

C++ Friend function

I don't understand whats the difference between using a Friend function vs making setter/getter functions. In the Sololearn examples, both ways can be used to access private data members. The only difference I see is how you call each one within the main function.

c++

3/23/2019 5:01:41 PM

J3fro C

8 Answers

New Answer

+10

Imagine you have a class named Person, that class has a member variable called age (of type int). Now you don't want anyone to add 1000 as his age, do you? No, you don't. But if you make that variable public then anyone can do somePerson.age = 1000 and this will cause trouble. So, that is why we make that variable private and create a public function called setAge() which will firstly check if given age is valid or not, if it is valid then only it will assign value to age variable. But now, as age is private, you cannot use that in the code (outside the class), so that is why you create a public function getAge() that returns the age.

+6

I feel that the answers here have explained friend functions and classes as well as getters and setters but no one has actually addressed the original question of what is the difference between using public setters/getters within the class and using public friend functions or classes.

+5

So is it safer to use public setters/getters than friend classes or functions as anything can aceess/modify private members of a class via friend functions/classes thereby undoing the benefit of encapsulation?

+3

Answer from this thread https://www.sololearn.com/Discuss/2201074/?ref=app Question is I need a simpler and clear explanation about why should/shouldn't I use friend function. I still don't quite understand the concept of OOP in C++. Is using friend function a bad practice? Does it reduce encapsulation? Why should I use friend function instead of changing my private members to public members? I found that f Is using friend function bad practice? - not really but still use it judiciously. Does it reduce encapsulation? - Popular to common belief NO It is as nearly as good as a member function but it is not a member function. To access private and protected member you need an object to access private and protected member. Why you need it? Mostly for operator overloading since some operators can be implemented only as non member function like stream operators "<<", ">>" Making members public makes them accessible to all. It is a concept that will become clear with experience. Avoid as much as you can.

+1

I know about access specifiers....friend function vs setter/getter functions. Thats all I want to know.

+1

Getter/setter function may be used by anything outside of class (if public). Friend function may access private/protected fields of the class even there are no setter/getter. https://code.sololearn.com/cCDK61GFSJgV/?ref=app

+1

Sonic There are no such thing like public friend functions or classes. Regardless where you declare friends, they have access to anything in class (including private/protected part). Public g/setters give access only to specific fields, rest of fields are still private or protected, while friends may access everything. Public g/setters may be used by anything outside class without any change in class, while to become friend, ones has to be added explicitly to class declaration. https://code.sololearn.com/cCDK61GFSJgV/?ref=app

0

Sonic You pointed at the most important thing i.e. encapsulation. Friends break it in some way, so should be used only if necessary. You may find friend useful in factory/builder functions when one class produces objects of other class, so needs access to private/protected fields. IMHO friend may be usually avoided, but... it is in use.