What is a friend class? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 9

What is a friend class?

C++

19th May 2019, 11:01 AM
Warwal
Warwal - avatar
5 Answers
+ 7
A friend class is a class that can access the private and protected members of the other class.
19th May 2019, 11:39 AM
HonFu
HonFu - avatar
+ 5
Refer link for the info on friend class - https://www.sololearn.com/learn/CPlusPlus/1899/
19th May 2019, 1:52 PM
Kuri
Kuri - avatar
+ 1
Friend class
20th May 2019, 3:11 PM
John Parsi
+ 1
I had never heard of a friend class until I reviewed some exam questions for a certificate focusing on C#. I thought it was a trick question, a fake class type, at first because it had never been used nor mentioned over the entire course. Is their use common in the real world and across languages? Thank you HonFu and Nagendra for your explanation and the link. The description somehow reminds me of a nested class. I still have not grasped how exactly it works. In the examples (link), the friend class looks on the one hand like a member of the "host" class, on the other hand the friend class is called on its own outside that class and not as a class member. I will have to re-read the article and Experiment with that some time. :-)
20th May 2019, 3:43 PM
Michael U.
Michael U. - avatar
0
Friend class is used to access private data member of one by second class if it is a Friend of that class
20th May 2019, 6:14 PM
Malhar Naiya
Malhar Naiya - avatar