- #1
ChrisVer
Gold Member
- 3,378
- 464
I know that this might seem a little stupid question, but I'd like to read some answers or maybe see some examples (better put as problems for my own practice)?
I don't understand how the concept of friend classes is useful in programing.
Classes, in my understanding, are supposed to be some kind of structures that allow you to create objects with certain attributes and methods on those objects. The attributes or methods can be private (accessed only from within the class) or public (accessed from outside the class), and recently I came across the protected but I am not sure how it really works yet but this is irrelevant.
Then I learned that friend classes are able to access the private elements of its class.
What is confusing me, is that I don't see the reason of making some elements of a class private if you want to access them in some other part of your code later on. Or define a friend class that you want to enter the private parts of your main class and modify them instead of doing that in your main class (this question may not receive an answer, since it's based on the contradiction arising between the concepts of friendship and encapsulation). Especially if you are building the code, I find the whole concept of friendship irrelevant (since there are ways to avoid it) and the code to be less readable to others (at least I can get confused reading through such a code).
I would appreciate your answers. Also, do you have some examples that can make the concept of friendship clear and necessary? I would appreciate the examples as "problems" (to make me think about it) rather than a copy-paste of some code.
Thanks.
I don't understand how the concept of friend classes is useful in programing.
Classes, in my understanding, are supposed to be some kind of structures that allow you to create objects with certain attributes and methods on those objects. The attributes or methods can be private (accessed only from within the class) or public (accessed from outside the class), and recently I came across the protected but I am not sure how it really works yet but this is irrelevant.
Then I learned that friend classes are able to access the private elements of its class.
What is confusing me, is that I don't see the reason of making some elements of a class private if you want to access them in some other part of your code later on. Or define a friend class that you want to enter the private parts of your main class and modify them instead of doing that in your main class (this question may not receive an answer, since it's based on the contradiction arising between the concepts of friendship and encapsulation). Especially if you are building the code, I find the whole concept of friendship irrelevant (since there are ways to avoid it) and the code to be less readable to others (at least I can get confused reading through such a code).
I would appreciate your answers. Also, do you have some examples that can make the concept of friendship clear and necessary? I would appreciate the examples as "problems" (to make me think about it) rather than a copy-paste of some code.
Thanks.