views:

66

answers:

1

what is the difference between friend function and friend class? and where should be use of friend keyword?

+3  A: 

In short, one is a class and one is a function. For the function, just that one function gets access to private members. For a class, the whole class and all its functions get access to the private members of the befriended class.

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

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

JoshD
`+1` for finding more to say about this than the first sentence. I would have utterly failed on that.
sbi