Friend function
Encyclopedia
A friend function for a class is used in object-oriented programming
Object-oriented programming
Object-oriented programming is a programming paradigm using "objects" – data structures consisting of data fields and methods together with their interactions – to design applications and computer programs. Programming techniques may include features such as data abstraction,...

 to allow access to , , or data
Data
The term data refers to qualitative or quantitative attributes of a variable or set of variables. Data are typically the results of measurements and can be the basis of graphs, images, or observations of a set of variables. Data are often viewed as the lowest level of abstraction from which...

 in the class
Class (computer science)
In object-oriented programming, a class is a construct that is used as a blueprint to create instances of itself – referred to as class instances, class objects, instance objects or simply objects. A class defines constituent members which enable these class instances to have state and behavior...

 from the outside. Normally, a function that is not a member of a class cannot access such information; neither can an external class. Occasionally, such access will be advantageous for the programmer. Under these circumstances, the function or external class can be declared as a friend of the class using the friend keyword.

A friend function is declared by the class that is granting access. Friend declaration can be placed anywhere in the class declaration. It is not affected by the access control keywords.

A similar concept is that of friend class
Friend class
A friend class in C++, can access the "private" and "protected" members of the class in which it is declared as a friend. On declaration of friend class all member function of the friend class become friend of the class in which the friend class was declared...

.

Friends should be used with caution. Too many functions or external classes declared as friends of a class with protected or private data may lessen the value of encapsulation
Separation of concerns
In computer science, separation of concerns is the process of separating a computer program into distinct features that overlap in functionality as little as possible. A concern is any piece of interest or focus in a program. Typically, concerns are synonymous with features or behaviors...

 of separate classes in object-oriented programming and may indicate a problem in the overall arcitecture design.

Use cases

This approach may be used when a function needs to access private data in objects from two different classes.
This may be accomplished in two similar ways:
  • a function of global or namespace
    Namespace
    In general, a namespace is a container that provides context for the identifiers it holds, and allows the disambiguation of homonym identifiers residing in different namespaces....

    scope may be declared as friend of both classes
  • a member function of one class may be declared as friend of another one.


  1. include

//using namespace std;

class B; // Forward declaration of class B in order for example to compile
class A
{
private:
int a;
public:
A { a = 0; }
void show(A& x, B& y);
friend void ::show(A& x, B& y); // declaration of global friend
};

class B
{
private:
int b;
public:
B { b = 6; }
friend void ::show(A& x, B& y); // declaration of global friend
friend void A::show(A& x, B& y); // declaration of friend from other class
};

// Definition of a member function of A; this member is a friend of B
void A::show(A& x, B& y)
{
cout << "Show via function member of A" << endl;
cout << "A::a = " << x.a << endl;
cout << "B::b = " << y.b << endl;
}

// Friend for A and B, definition of global function
void show(A& x, B& y)
{
cout << "Show via global function" << endl;
cout << "A::a = " << x.a << endl;
cout << "B::b = " << y.b << endl;
}

int main
{
A a;
B b;

show(a,b);
a.show(a,b);
}

External links

The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK