THE FRIENDS OF CLASS

Sometimes it can be necessary that some function or functional member or even entire class have rights to access private or protected members of some other class. Specific class in that case can be added to specific function, functional member of some class or even to entire class the right of access to all the private and protected members. The part of the program in which has been granted the right of access is named a friend of the class.
Let’s define for example a function which will add up two vectors. The function can do that using public interface of vector but it’s more natural to access directly to members of the class Vector. Class can be redefined in the following way:
class Vector {
       friend Vector AddVectors(Vector a, Vector b);
private:
       float ax, ay;
public:
};
Vector AddVectors(Vector a, Vector b){
       Vector c;
       c.ax = a.ax + b.ax;
       c.ay = a.ay + b.ay;
       return c;
}
Declaration of some function as a friend starts using keyword friend after which we have to type the full function prototype in which we have to put the access rights. It’s not necessary to declare the function before the function becomes the friend of the class. In our example the function AddVectors is declared and defined after declaration of the class. If we have to make more than one function a friend of the class than each function is specified separately. It’s not possible after one keyword friend to specify more than one function declaration.
The right of access is possible to assign to certain functional members of other classes. So it’s possible to define class Matrix which will contain functional member which multiplies the matrix with vector. The class Vector will assign the right of access to functional member Matrix::Multiplywithvector() in the following way:
class Vector;
class Matrix{
private:
       float a11, a12, a21, a22;
public:
       Vector Mutliplywithvector(Vector &v);
};
class Vector{
       friend Vector Matrix::Mutliplywithvector(Vector &v);
};

Vector Matrix::Mutliplywithvector(Vevotr &v){
       Vector rez;
       rez.ax = a11*v.ax + a12*v.ay;
       rez.ay = a21*v.ax + a22*v.ay;
       return rez;
}
During the assignment of access rights to functional member of class there is a rule that right cannot be granted if a member function is not previously declared. So the declaration of class Matrix preceded the declaration of a class Vector which achieves that in the moment of declaration of the class Vector functional member that must have access rights is already declared. Definition of functional member Matrix::Multiplywithvector() is placed after the definition of the class Vector for two reasons:
-          Structure of class Vector must be known in order to have access to his members
-          Class Vector must grant access to functional member in order to access private members.
Sometimes it’s necessary to grant access to all functional members of some class. Then whole class can be declared to friend of second class. In that case that class have access to all private and protected types and nested classes. We can do that by typing friend class after which we put the name of the class and after that the rights are assigned. The class to which are assigned the access rights must be declared in the moment of allocation even incomplete. To illustrate this let’s take a look at following example:
class Matrix;
class Vector{
       friend class Matrix;
};
All private and protected data types are defined inside the class Vector and now can be used in definion of functional members of class Matrix. Inside the declaration friend function can define – then the function will be inserted. This is very similar with functional members which can also be defined in declaration and in that way they become inserted. For example function AddVectors() can be defined in the following way:
class Vector {
       friend Vector AddVectors(Vector a, Vector b){
             Vector c;
             c.ax = a.ax + b.ax;
             c.ay = a.ay + b.ay;
             return c;
       }
private:
       float ax, ay;
public:
       //...
};

Function addvectors() will be inserted, which means that the program code will be executed faster. Although it’s defined inside the class it’s important to notices that this function is not part of the class.
Share on Google Plus