what is the difference between friend function and friend class? and where should be use of friend keyword?
In short, one is a class and one is a function. For the function, just that one function gets access to private members. For a class, the whole class and all its functions get access to the private members of the befriended class.
The friend keyword is used to grant access to private data members. At times you may need a helper class or a complimentary class to access the private members of a different class. For functions, a common example is an operator overload. Perhaps you want to overload the + operator. You may make an operator+ function declared outside the class (so it can be called without an object) and it will need to access the private class data.
Check out this site for a detailed description of both and how to use them.
friend
keyword is used for declaration.friend
keyword is not required.friend
keyword is used: friend class a;
friend
keyword is not required.friend
class we can access the members of one class into another.A friend function is used for accessing the non public member of a class.A class can allow non-member function and other classes to access its own private data by making them friend A Friend class has full access of private data members of another class without being member of that class.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With