class x { void xx() {} }; class y { friend void x::xx(); };
This results in an error like
error: friend function 'xx' is a private member of 'x'
Why can't I declare a private member function to be a friend of another class?
A function or class can't declare itself as a friend of any class. In a class definition, use the friend keyword and the name of a non-member function or other class to grant it access to the private and protected members of your class. In a template definition, a type parameter can be declared as a friend .
A friend function cannot access the private and protected data members of the class directly. It needs to make use of a class object and then access the members using the dot operator. A friend function can be a global function or a member of another class.
A friend function can be declared in the private or public part of a class without changing its meaning. Friend functions are not called using objects of the class because they are not within the class's scope. Without the help of any object, the friend function can be invoked like a normal member function.
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.
This function has access to private and protected members of the same class. One can call the friend function in the main function without any need to object. One has to create an object of the same class to call the member function of the class. The Friend keyword is generally used to declare a function as a friend function.
Friend class and function in C++. Friend Class A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful to allow a particular class to access private members of other class.
To declare a friend function, it’s prototype must be declared inside the class, preceding it with keyword “ friend ”. Check Friend Function for more information. A Member function is a function defined in the class as a member of the function.
Friend Function Like friend class, a friend function can be given a special grant to access private and protected members. A friend function can be:
[class.friend]/9:
A name nominated by a friend declaration shall be accessible in the scope of the class containing the friend declaration.
The reason is quite simple; private
members shall obey a clear and definite rule:
A member of a class can be
private
; that is, its name can be used only by members and friends of the class in which it is declared.
Allowing private members to be named in declarations inside unrelated classes would violate this rule: it enables another class to depend on an implementation detail without being explicitly allowed to. This becomes problematic, for instance, when changing a private member's name, type or signature, or removing it entirely; that's intended not to break the interface of that class.
This can be circumvented by making the entirety of x
a friend of y
:
class x { void xx() {} }; class y { friend x; };
Demo.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With