Friend Specifier
As we have already seen, private and protected members of a class are not accessible from within other functions and classes. A class can declare another function or class as a friend: this function or class will have access to the private and protected members of the class which declares the friend relationship.
The user has to specify the friend declaration within the body of the class.
Friend Functions
Friend functions are non-member functions that are entitled to access the private and protected members of a class. The way to declare a function as a friend function is by adding its declaration within the class and preceding it by the friend keyword. Let's examine the following code:
class class_name { type_1 member_1; type_2 member_2; public: friend void print(const class_name &obj); }; friend void print(const class_name &obj){ std::cout << obj.member_1 << " " << member_2 << std::endl; }
In the previous example, the function declared...