What is friend function in OOP

C ++ to a friend or not to a friend

Jason Derulo - Wilde Liebe (Prod. Jawsh 685) (Text) 🎵

I did an object-oriented programming course in C ++ in college this semester and we learned about friend functions.

I instinctively don't like them because they can bypass the security offered by Encapsulation and Data Hiding. I've read some articles on the internet and some people thought it was a good idea with some legitimate uses.

What would an OOPs expert say about friends' functions in C ++? Should I just skim it or find out more about it?

2
  • @all: Great answers and comments. This is a great way to learn. There was no way I would have found out about friends in such detail in a textbook.
  • See: programmers.stackexchange.com/questions/99589/…

It is not always convenient to make all functions related to a C ++ class members of that class. For example, imagine an implementation of vector algebra with scalar multiplication. We want to write:

We can do this with a membership function:

But we would also like to write:

This requires a free function:

A keyword was added to the C ++ to support this usage. The free function is part of the implementation of the Vector class and should be declared in the same header and implemented in the same source file.

Likewise, we can use simplifying the implementation of tightly coupled classes like a collection and an iterator. Again I would declare both classes in the same header and implement them in the same source file.

4
  • 3 "This requires a free function". No, it doesn't:. Canonical solution indeed.
  • 1 @MSalters: Good point. I picked a bad example. I think your inline function is free function by definition, but it doesn't require a declaration of friend.
  • 4 @MSalters: This only applies if * is commutative with respect to a and v (x). If the vector components are generic (not necessarily scalars), you must maintain the order of the operands
  • That's pretty theoretical. Maybe the only common non-commutative case would be and that still doesn't require friendship.

Friend functions are no different from member functions in terms of encapsulation. However, they can offer other benefits, such as: B. to be more general, especially when it comes to templates. In addition, some operators can only be specified as free functions. So if you want them to have member access, this is what you need to do.

It is better to have a single function than to be forced to do something you don't want to make public. That means the whole world can use it - instead of just one function.

2
  • +1 for Friend functions are no different from member functions in terms of encapsulation. However, this only applies to public member functions.
  • 1 @TheFogger: You could probably also do a function that is also "private", as it is only declared in a single TU.

'What would an OOPs expert say ..."It mainly depends on how experienced he is in C ++ that - by his own specification - is not a language for purists (and does not want to be).

OOP Zealots don't use C ++ (they prefer small talk and like Java).

Functional programming zealots don't use C ++ (they prefer LISP and its successors).

Most OOP professionals don't like the function of friends just because they want the OOP part of C ++ to act like small talk. But C ++ isn't small talk, and they can't even understand that Friend don't break the encapsulationfor the very simple reason that A function cannot be a friend of your class without your class requesting it.

And from the standpoint of "functionality" between and there is no difference (where is a friend): the parties are the same. Simply put, one syntax may be more appropriate than another: if fn is commutative with respect to and, then arguably more appropriate (where an appearance has a "special role" that it actually doesn't.)

3
  • 1 "OOP fanatics" who like Java did not understand OOP. Getters? Setter? No simple syntax for closures? To paraphrase Alan Kay, that's not how he imagined OOP.
  • @ Konrad: Zealots are superior and unlimited. There is always one more zealot than one particular zealot.
  • I have to say that I voted positively because I really liked this last paragraph. Makes a lot of sense.

Does "friend" violate encapsulation?

No it does not. Like membership, "friend" is an explicit mechanism for granting access. You cannot grant yourself access to a class (in a standards-compliant program) without changing its source.

If you are passionate about what you do, learn all about C ++. Learn what they're used for, how they're used, and then decide - and only then - not to use them. At the very least, you'll be prepared to read someone else's code using this facet of C ++.

The C ++ FAQ is short and sweet:

Use a member if you can and a friend if you have to.

The FAQ provides one of the more useful ways of thinking about friendship:

A lot of people think that a friend is something out of class. Instead, try to think of a friend function as part of the class's public interface. A friend function in the class declaration does not violate encapsulation any more than a public member function violates encapsulation: Both have exactly the same authorization for access to the non-public parts of the class.

Perhaps the most common use of friend functions is overloading < für="" e="" a.="">

Friend functions are best used for custom operator definitions. They are useful in other situations. However, if you do give friend classes a lot, you may find yourself on a design detour (just a good self-exam to use when writing code).

Be careful about the "Security Statement" in the original question. Access modifiers prevent you from accidentally writing bad code, just like the compiler in some ways. Access modifiers restrict the interface and are used to communicate which functions are important for the use of the class (public and protected) and which were created to make the class more beautiful for the maintainer (private). Modifiers are not a security measure because there are many ways to access private data. For example, get a pointer to the class and its size and go fishing.

C ++ friend functions are closely related to the following functions:

  1. free functions
  2. static functions
  3. Friend functions

This means that they do not have this pointer and are therefore outside the class / object. On the other hand, they often take parameters that make them class again. Here is an example that clarifies the link:

The only difference between static functions and friend functions is that a friend function can use multiple classes.

Using the friend mechanism in C ++ requires programmers who have approximately 10 to 15 years of C ++ programming experience. Hence, you should avoid this first. It's an advanced feature.

6
  • 7 And you have 10-15 years how derived?
  • 10-15 years come from the point in time when it actually becomes necessary.
  • 3 So you came up with a random number.
  • 3-1: "You should avoid it." All of the functions of C ++ were created to solve a problem. If you encounter this problem, use the appropriate function.
  • Thanks for the -1. There was a reason for this comment. I think it's just a difficult concept that not all features are suitable for beginners.