Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't a PRIVATE member function be a friend function of another class?

Tags:

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?

like image 370
Smatik Avatar asked Nov 16 '14 10:11

Smatik


People also ask

Can member function be friend function 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 .

Can a friend function access private members of a class?

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.

Can we declare friend function as private?

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.

Can we define friend function outside the class?

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.

How to call a member function of the same class?

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.

What is friend class and function in C++?

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.

What is the difference between friend function and member function in JavaScript?

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.

What is friend function in Java?

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:


1 Answers

[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.

like image 189
Columbo Avatar answered Oct 06 '22 16:10

Columbo