C++ Friend Functions

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

C++ - FF

FRIEND OR FOE?
Friends differ from other people in the fact that they have
access to many aspects of our private world. They know
our secrets, feelings, passwords to mail accounts or even
credit card PINs. This is possible and reasonable because we
trust them.
So far we showed classes as very wary and unbelieving
creatures. They have many private things inaccessible to
others. They watch their possessions to keep them protected.
It may seem that classes are poor, lonely and a bit paranoid
bodies. Fortunately, its not true. Classes may have friends too. We are going to tell you about it now.
A friend of a class may be:
a class (it is the so-called friend class)
a function (it is the so-called friend function)
A friend (class of function) is able to access those components which are hidden from any others.
Friends are allowed to access or to use private and protected components of the class.
A friendship in this sense is like a permit and an expression of confidence.
HOW TO SAY IT?
If there are two classes named, for example, A and B, and if A wants B to be able to access its private
possessions, it has to announce that B is its friend. The announcement works in one direction only.
The B class mustnt say I am As friend it wont work.
Note: its just like in the real word. Our friends are those who we refer to as friends. People claiming
that they are our friends may be liars.
Take a look at the example
A class named Class announced that Friend is its friend. In effect, an object of the Friend class is able
to manipulate the Class private fields and invoke its private methods.
Note: it doesnt matter where you put the friendship declaration, i.e. the line starting with the
phrase:
friend class ;
It may exists inside any of the class parts (public, private or protected) but must be put outside any
function or aggregate.
The presented program outputs:
It's a secret, that field = 100
#include <iostream>
using namespace std;
class Class {
friend class Friend;
private:
int field;
void print(void) { cout << "It's a secret, that field = " << field << endl; }
};
class Friend {
public:
void DoIt(Class &c) { c.field = 100; c.print(); }
};
int main(void) {
Class o;
Friend f;
f.DoIt(o);
return 0;
}

SS Puram, Tumkur | M.G. Road, Tumkur | Ph: +91-9620160796

C++ - FF
THE RULES
There are some additional rules that must be taken into account:
a class may be a friend of many classes
a class may have many friends
a friends friend isnt my friend
friendship isnt inherited the subclass has to define its own friendships
Consider the following example Try to predict its output.
Yes, its It's a secret, that field = 111.
#include <iostream>
using namespace std;
class A {
friend class B;
friend class C;
private:
int field;
protected:
void print(void) { cout << "It's a secret, that field = " << field << endl; }
};
class C {
public:
void DoIt(A &a) { a.print(); }
};
class B {
public:
void DoIt(A &a, C &c) { a.field = 111; c.DoIt(a); }
};
int main(void) {
A a; B b; C c;
b.DoIt(a,c);
return 0;
}
FRIEND FUNCTIONS
A function may be a class friend too. Such a function may access all the private and/or protected
components of the class.
The rules are a bit different than previously:
a friendship declaration must contain a complete prototype of the friend function (including
return type); a function with the same name but not compatible in the sense of parameters
conformance will not be recognized as a friend
a class may have many friend functions
a function may be a friend of many classes
a class may recognize as friends both global and member functions
Take a look at the example
The A class has three friends. They are:
1. the B class
2. the global DoIt() function
3. the member function DoIt (from the C class)
Note the line:
class A;
on the top of the source file. It does nothing except informing the compiler that a class named A will
be in use. The lack of the line will cause compilation errors as the compiler wont be aware of the A
class existence when analysing the C class body.

SS Puram, Tumkur | M.G. Road, Tumkur | Ph: +91-9620160796

C++ - FF
You wont solve the problem by moving the A class to the top of the source file. Do you know why?
The example program writes:
It's a secret, that field = 99
#include <iostream>
using namespace std;
class A;
class C {
public:
void dec(A &a);
};
class A {
friend class B;
friend void C::dec(A&);
friend void DoIt(A&);
private:
int field;
protected:
void print(void) { cout << "It's a secret, that field = " << field << endl; }
};
void C::dec(A &a) { a.field--; }
class B {
public:
void DoIt(A &a) { a.print(); }
};
void DoIt(A &a) {
a.field = 99;
}
int main(void) {
A a; B b; C c;
DoIt(a);
b.DoIt(a);
return 0;
}

SS Puram, Tumkur | M.G. Road, Tumkur | Ph: +91-9620160796

You might also like