answersLogoWhite

0


Best Answer

Declaring a function to be a friend of a class allows the friend function to gain private (and protected) access to that class, just as it would if it were declared a static member function of the class. Most of the time you will want to use static functions rather than friend functions, but sometimes it is necessary for a member method of another class or an external function to be permitted private access. Although some will tell you this undermines the encapsulation of the class, it does not. If anything, it reinforces that encapsulation, by only allowing the specified function to gain private access. Although the friend function is not physically a member of the class, it automatically becomes tightly bound to the class. That is not necessarily a bad thing when you're only dealing with a limited number of friends, but a class that has many friends can often be an indication of poor class design. Thus it is important to limit friendship and to only use it when it is absolutely required, whenever possible.

By way of an example, suppose you have designed a parent and child class. Even without friend functions, the two are implicitly bound to each other: the parent object is a container for child objects, while child objects refer to their parents. All work is delegated and coordinated between the two classes accordingly; they work together as one. Most of the work can be encapsulated without the need to explicitly bind the classes together, however child objects typically refer to their parent object (via a member pointer) and might also be permitted to change their parent, but we wouldn't want to expose that pointer outside of the class, nor would we want to expose any mutators such as child::set_parent() outside of the class. But since the two classes are imlicitly bound to each other, we can declare specific methods of the parent class to be friends of the child class, such as parent::add_child(). In this way we can be sure that whenever we add a child to a parent, the parent not only attaches the child to itself, but also detaches the child from any existing parent beforehand. Far from undermining the encapsulation, this friendship reinforces it, assuring us that both classes are tightly bound.

User Avatar

Wiki User

10y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

10y ago

Friend functions are required whenever you have a function that cannot logically be declared a member of the class, but that still requires private access to the class members. In most cases, private access (and therefore protected access) to the class members would be undesirable in an external function; members are declared private or protected for a reason. However, if the class interface is such that the external function cannot fulfil its role and the interface cannot be modified because it would undermine the encapsulation, then the class may declare the function to be a friend.

The function itself has no access to the implicit thispointer, thus it must accept an instance of the class by reference (where an instance is guaranteed to exist for every call) or by pointer (if an instance may or may not exist). The function can then indirectly access the private members of the class via the reference or non-NULL pointer.

Friends functions are often employed whenever two (or more) classes must work closely together in such a way that an operation upon any one instance of a class must automatically invoke private or protected operations upon one or more of the others. For instance, in a parent/child structure, the parent often contains a private embedded list of its children while each child contains an embedded read-only pointer to its parent. Thus when adding a child to a parent, it would be desirable for the parent::add_child(child&) method to automatically modify the private child::m_parent member of the given child. Therefore the parent::add_child() method should be declared a friend of the child class.

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

A friend function in C++ is a function that is not part of the implementation of a class, i.e. not a method of the class, but that is allowed to manipulate the private parts of a class.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Need of friend function in c plus plus?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What is the code to perform friend function in C plus plus?

Something like this:


What is the operator of power in c plus plus?

There is no "power" operator in C or C++. You need to the use the math library function pow().


What is the difference between constructor and friend function in c plus plus?

A constructor is a method that fires when the object is instantiated. A friend function is a function that has special access to the object. They are two different types of things, and cannot be further differenced.


What is friendly in c plus plus?

The keyword "friend" allows a function or variable to have access to a protected member inside a class.


What is the use of private constructor in c plus plus?

Private construction prevents objects from the class from being instantiated other than via a static member function of the class, a friend function or a friend class.


What are the building function in c plus plus?

There is no such term as "building function" in C++.


Can there be friend functions in c plus plus?

Yes, there can be friend functions in C++.


How do you correct the C plus plus programming error missing function header?

You need to #include the header file that contains the missing function's declaration.


What are the limitations of friend function in c plus plus?

Only that they cannot be inherited by derived classes. This is "a good thing". Other than that, a friend function has full access to a class' private and protected members and you cannot limit its scope. At this data hiding feature of c++ is broken.


What is the difference between friend function and inheritance in c plus plus?

There is no such thing. When declaring a friend function only the explicitly-scoped friend is granted private access. The friend function may well be declared virtual within its own class but none of its overrides are granted access unless they are explicitly granted access.


A c plus plus statement that invokes a function is known as?

...a function call.


When will you make a function inline in c plus plus?

yes,we can make function inline