1

What is the real use of a friend function/class in C++? Could you give an example where only friend is the right approach?

Thanks

4
  • 1
    See those questions on the right-hand-side of the page? Click through a few of them. They are relevant.
    – Blender
    Dec 23, 2011 at 5:13
  • 1
    Yes, got a good one here : stackoverflow.com/questions/17434/…. I should have done some more research before posing my question, sorry :-(
    – Sanish
    Dec 23, 2011 at 5:29
  • Extending the public interface and Documenting coupling: programmers.stackexchange.com/a/99595/12917 Dec 23, 2011 at 6:06
  • Unlike enemies - you need to keep your friends closer. Stay clear of friends and do not trust them with your assets (BTW That was a metaphor for the Americans around here)
    – Ed Heal
    Dec 23, 2011 at 6:39

3 Answers 3

8

"In C++, only your friends can access your private parts."

The point of a friend is that you can package your software into smaller groupings, like friend classes etc, while still allowing access to the internals of a class. This arguably lets you maintain finer control on encapsulation than without friending.

7
  • 2
    You must come from California?!
    – Ed Heal
    Dec 23, 2011 at 5:15
  • Oh those classes! Always experimenting with their friends! (Your quote made me actually lol :p)
    – Corbin
    Dec 23, 2011 at 5:16
  • I've been using that since I taught C++ 15 years ago and it's always been a fav fave. Thanks. Dec 23, 2011 at 15:38
  • @EdHeal Why? (Actually I'm from Colorado but I've lived in California.) Do people outside California let someone who isn't their friend access their private parts? Dec 23, 2011 at 15:40
  • 1
    @pinkpanther - It is a British thing called humour
    – Ed Heal
    Jun 23, 2013 at 12:20
0

i think often friend function usage its a matter of readability. For example next overloaded operator looks symmetrically and this may be good for code reader.

friend const X operator+(const X&, const X&);

inline const X operator+(const X& arg1, const X& arg2)
{
    X r;
    r.x = arg1.x + arg2.x;
    return r;
}

But main point its their ability to access private data of two different classes.

-2

Using friend is actually very discouraged in C++ (it kinda breaks the whole encapsulation idea), but it comes to my mind an example where only friend is the right approach:

friend ostream & operator<< (ostream & out, const MyClass & C);
friend istream & operator>> (istream & in, MyClass & C);
21
  • 1
    Why are things different with input and output operators? If you're going to give the user the ability to read/write the class data from/to a stream, why not give them public accessors to the data? Then the operators don't need to be friends, since they can call the public accessors. Dec 23, 2011 at 5:49
  • 1
    friendship increases encapsulation. Dec 23, 2011 at 6:08
  • there are plenty of cases where you want the class to have iostream behaviour, but you don't want to give public accessors to some attributes. It may sound contradictory.. because it is, and that's why friend is discouraged. Dec 23, 2011 at 6:10
  • @LokiAstari: how could friendship increase encapsulation? It decrease encapsulation at any point of view. Encapsulation means restricting access to some classes components. Using friend, you ignore that restricted part of a class, so, it goes against encapsulation. Dec 23, 2011 at 6:16
  • 1
    @julio.alegria: It increases encapsulation because it reduces your need to expose your internal implementation in general. Though you are exposing you internals yo your friends (and thus tightly couple yourself to your friend) this will reduce the need to expose your internal implementation to non friends (ie people that are not part of your public interface) this reduces overall coupling and thus increases encapsulation. Dec 23, 2011 at 6:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.