1

Possible Duplicate:
When should you use 'friend' in C++?

So I know a class or function can be keyworded as friend but what does that mean and how do I use it? I've tried reading around but no one explains what the following means:

class x{
public:
friend function(int x);
};

Why do i declare function a friend, what use does that give me. Also, what does it mean to declare a class a friend? thanks.

Community
  • 1
  • 1
Richard
  • 5,840
  • 36
  • 123
  • 208

2 Answers2

0

A friend function is basically declared in a class, but defined outside of it. It is given special access to class's otherwise restricted (ie: private) internal members.

See: http://www.cplusplus.com/doc/tutorial/inheritance/

Josh
  • 12,448
  • 10
  • 74
  • 118
  • Does it HAVE to be defined outside? That's where I went to try to understand friend but it was no help. What is the use of a friend function? – Richard Nov 01 '11 at 04:52
  • @Richard: Did you read that article? The top chunk of it describes it pretty clearly. If you wouldn't want to define it outside of it, then it'd just be a method of the class. – Josh Nov 01 '11 at 04:53
0

Do you know what the private and protected keywords mean?

friend functions, and all member functions of friend classes, bypass the accessibility checks.

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720