What is a friend function?

This tip submitted by Ali Nawkhas Murad on 2012-02-23 00:36:16. It has been viewed 11185 times.
Rating of 6.4 with 94 votes



In general an object's attributes should be hidden from the outside. Sometimes, however, other non-member functions need to be able to access the private members of a class. In this case, the non-member function must be designated as a friend function of the class. Thus the friend function be able to access all members of the object, even protected members of the base class of the object.

The friend function declaration form is as shown below:

friend return-type function-name(parameter list);

Of course the implementation can be done outside the class.

Read more about friend functions.



More tips

Help your fellow programmers! Add a tip!