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

23rd Mar 2019, 5:01 PM
J3fro C
J3fro C - avatar
8 odpowiedzi
+ 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.
23rd Mar 2019, 5:23 PM
Letsintegreat
Letsintegreat - avatar
+ 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.
6th Apr 2019, 4:21 AM
Sonic
Sonic - avatar
+ 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?
8th Apr 2019, 6:44 AM
Sonic
Sonic - avatar
+ 1
I know about access specifiers....friend function vs setter/getter functions. Thats all I want to know.
23rd Mar 2019, 6:46 PM
J3fro C
J3fro C - avatar
+ 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
29th Mar 2019, 8:38 AM
px16
px16 - avatar
+ 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
8th Apr 2019, 6:06 AM
px16
px16 - avatar
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.
8th Apr 2019, 11:59 AM
px16
px16 - avatar