answersLogoWhite

0

What is friendship in c plus plus?

Updated: 8/21/2019
User Avatar

Wiki User

9y ago

Best Answer

Whenever you declare a member function, that function has three properties:

1. The function can access the private representation of the class.

2. The function is in the scope of the class.

3. The function must be invoked on an object (it has a this pointer).

When you declare a static member function, that function has the first two properties only. When you declare a nonmember function to be a friend of the class, that function has the first property only.

When you first begin designing classes it is often tempting to declare every function that uses the class to be a member of that class. However, it is important to keep class interfaces as clean and as simple as possible. Aside from the class constructors and its destructor, certain functions must always be declared members, such as assignment and compound assignment operators, conversion operators, setters and getters. But all other functions must be carefully considered. For instance, overloading the + operator as a class member is not necessary if the class already has a public copy or move constructor and a public += operator. It can simply be declared a nonmember function:

foo operator+ (foo a, const foo& b) { return a += b; }

Note that argument a is passed by value (and is therefore copied or moved automatically) while argument b is a constant reference. The + operator is implemented in terms of the public += operator, returning the result by value (which is again copied or moved automatically). Declaring this operator as a member function offers no advantages whatsoever, it would simply pollute the foo namespace unnecessarily.

In effect, the + operator overload becomes part of the global namespace -- which is exactly where it belongs because it does not require any of the three properties listed at the beginning of this answer. Therefore, when deciding upon a function's membership, always ask which of the three properties it requires. If the answer is none, then it is a nonmember function.

Friendship comes into play when a nonmember function requires the first property only. This could be because the public interface is tightly constrained (as it should be) and modifying it to suit just one nonmember function would be considered undesirable (exposing far more than it really needs to). However, the most common reasons for friendship are because the function is already member of another class (no function can be a member of more than one class) or where the function requires private access to two or more classes. In some cases, two separate classes might form a unified unit (such as parent and child classes) where each needs private access to the other. So long as the first property is the only requirement, friendship is the preferred option.

There are some who believe friendship undermines encapsulation, however this is errant nonsense. Although a friend is not a member of the class that declares it to be a friend, it is still considered part of the class interface, just as the nonmember + operator is considered part of the foo interface. The only difference is that friends require access to the private representation of the class. As such, they must obey the encapsulation rules and maintain any and all invariants of the class, just as any member function must. You are in control of the design and implementation of both the class and its friends. The only way you can undermine encapsulation is by allowing friend access to a function you have no control over.

User Avatar

Wiki User

9y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is friendship in c plus plus?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions