DEFINITION 8: FRIENDS - PRATMG/2143-OOP-Tamang GitHub Wiki
Friend Feature A friend function, can be granted a specific permission to access private and protected members of the class in C++. They are defined globally outside the class scope. Friend functions are not member functions of the class. A friend function can be one of the following:
- a member of another class.
- a global function.
#include <iostream>
class A {
int a;
public:
A() { a = 0; }
// global friend function
friend void showA(A&);
};
void showA(A& x)
{
// Since showA() is a friend, it can access
// private members of A
std::cout << "A::a=" << x.a;
}
int main()
{
A a;
showA(a);
return 0;
}
REFERENCE: