A friend function of a class is defined outside that class' scope but it has the right to access all private and protected members of the class. Even though the prototypes for friend functions appear in the class definition, friends are not member functions.
A friend can be a function, function template, or member function, or a class or class template, in which case the entire class and all of its members are friends.
To declare a function as a friend of a class, precede the function prototype in the class definition with keyword friend as follows:
class ABC
{
double a;
public:
double b;
friend void printWidth( ABC abc );
void setWidth( double c );
};
To declare all member functions of class XYZ as friends of class ABC, place a following declaration in the definition of class ABC:
friend class XYZ;
No comments:
Post a Comment