When is it wise to use friendships in OOP?

I am currently browsing http://www.cplusplus.com and I came across this section here: http://www.cplusplus.com/doc/tutorial/inheritance.html , which focuses on the topic of friend functions and friend classes in C ++.

My question is when When is it wise to use friendships when creating a program?

The only key I received was in the example inside the article, which demonstrated the function of a friend who "duplicated" the object.

+4
source share
5 answers

There are some really good rules for this in the Marshall Cline: C ++ FAQ Lite .

All this is good, but, in particular, see "Do friends break encapsulation?" on examples of the correct way to use them and when it is better to separate classes and declare them friends.

+5
source

Friend functions exist for representing free functions as an integral part of a class interface. There are several places where free functions are part of the class interface. Example. Suppose you have a BigNum arbitrary precision BigNum . Here are some obvious candidates for friends functions:

 // binary operators where BigNum isn't the left-hand operand BigNum operator+ (int, BigNum); BigNum operator- (int, BigNum); // stream operators std::ostream &operator<< (std::ostream &os, const BigNum &num); std::istream &operator>> (std::istream &is, BigNum &num); 

Now, given these two examples, in many cases the binary operators do not need to be friends (for example, I can implement int + BigNum delegation to BigNum + int , which is a member function and therefore already has full access), but it all depends on your performance needs and what you are willing to provide through the functions of a public member of the class.

+4
source

One of the good applications for friend classes is the Memento Design Template.

+3
source

Since friends usually violate data hiding mechanisms, they should be used only when it is really necessary. If your design is heavily dependent on friends, it is probably wrong in some way.

An example of when you cannot do without friends is to redefine <<and β†’ stream operators. In addition, friend classes are often used when implementing some design patterns - an β€œIterator” comes to mind.

0
source

I used only the methods and properties of a friend to use inside the class itself to clone or assign another object to myself. This has been largely replaced, we reorganized the design to implement the Memento Design template in our design.

Memento is created by the same mechanisms that are used to save and load the object. that is, Memento creates a stream, an object writes to it, and memory can be transferred to any other object of the same class to make it an exact duplicate of the object that creates the memory.

Sometimes there are cases when objects should work very close to each other, and in this case I simplify their interaction by combining them behind another object. Variables that are "friendly" variables and are visible only to other objects are private to the class that performs the aggregation.

0
source

All Articles