Friend Function in C++ Language
Friend Function in C++ Language
What is Friend Function ?
2-Friend function is declared in the class with friend keyword
3-It must be defined outside the class to which it is friend.
4-Friend function can access any member of the class to which it is friend.
5-Friend function cannot access members of the class directly.
6-It has no caller object.
7-It should not be defined with membership label.
Example :
As we are seeing in quotation given above, the friend fun() has been called without caller object i.e fun() and the friend function declared inside Class but defined outside the Class.
Lets discuss how friend function can become friend to more than one class
As we are seeing in quotation given above, the friend function can access both private member of class i.e A and B together with the help of object i.e fun(A o1,B o2).
But there is an error in the quotation above. Let's see what the error is and how to remove it:-
The compiler is giving an error which means that the compiler does not understand the meaning of the letter B in the Friend function i.e friend void fun(A,B);
Now lets see how to remove this error :-
In order to solve the above error, we will have to make a forward class declaration i.e class B that the compiler can know the difference in both the B.
Comments
Post a Comment