What is the difference between friend function and friend class?

What is the difference between friend function and friend class? and where should the friend keyword be used?

+5
source share
5 answers

In short, one is a class and one is a function. For a function, only one function gains access to private members. For a class, the entire class and all its functions gain access to private members of a friend class.

The friend keyword is used to provide access to private data. Sometimes you may need a helper class or a free class to access private members of another class. For functions, a common example is operator overloading. Perhaps you want to overload the + operator. You can make an operator + function declared outside the class (so that it can be called without an object), and it will need to access the data of the private class.

Check out this site for a detailed description of both how and how to use them.

+11
source

Friend function

  • The keyword is friendused for the ad.
  • When writing a function definition, a keyword is friendnot required.
  • .

  • friend friend: friend class a;
  • friend .
  • friend .
+1

. , , , Friend , .

+1

1) Friends , - .

2) - , - .

3) , , - , .

0

Friend .

, , ( ) , .

I have already explained the difference between the two using examples on my blog: The difference between the function of a friend and the class of a friend

0
source

All Articles