176

Is there a difference between declaring a friend function/class as private or public? I can't seem to find anything about this online.

I mean the difference between:

class A
{
 public: 
      friend class B;
 };

and

class A
{
 private: //or nothing as the default is private
      friend class B;
 };

Is there a difference?

Daniel Duque
  • 159
  • 9
BIU
  • 2,340
  • 3
  • 17
  • 23
  • 34
    Such misinformation... someone didn't deserve to be a friend. It's entirely up to you whether you like your friends tucked in with your privates. – Tony Delroy Jun 20 '11 at 06:55
  • may i ask what a friend class is :'(? – I Phantasm I Jun 20 '11 at 06:58
  • 6
    @I Phantasm - it's a declaration that allows an instance of the `friend` class to access the members declared `private` in the class that made the declaration. In the case of this example, an instance of class B can access the private members of class A – BIU Jun 20 '11 at 07:02
  • 2
    possible duplicate of [Does it make a difference whether I put 'friend class xxxxx' in the public or private section?](http://stackoverflow.com/questions/2722222/does-it-make-a-difference-whether-i-put-friend-class-xxxxx-in-the-public-or-pri) – Suma Jun 20 '11 at 07:42
  • 3
    This question has earned me way too many points on this site. All right then. – BIU Mar 12 '14 at 11:51

4 Answers4

170

No, there's no difference - you just tell that class B is a friend of class A and now can access its private and protected members, that's all.

sharptooth
  • 167,383
  • 100
  • 513
  • 979
  • 5
    so I guess whoever told me that just didn't know what they were talking about. Thanks :) – BIU Jun 20 '11 at 06:54
  • 1
    but for documentation purposes, would you consider a `friend` an implementation detail or part of the interface? – TemplateRex Aug 24 '14 at 18:38
  • 1
    @TemplateRex: IMO that's part of interface - it's quite a strong claim that there's some (random) `class Friend` which can access all private members of the current class. – sharptooth Aug 25 '14 at 06:45
  • for random class, yes. But say you implement `operator==(T, T)` using private data members of `T`, and use `friend` as an implementation detail so that `operator==` can appear as a non-member. IMO, this friendship should not appear in the public interface (as will be generated by Doxygen e.g.) – TemplateRex Aug 25 '14 at 06:49
  • I read some time ago here in stackoverflow that some compilers, I guess some old buggy ones, will get confused if a friend declaration happens within a non-public region. – ABu Aug 27 '19 at 23:35
43

Since the syntax friend class B doesn't declare a member of the class A, so it doesn't matter where you write it, class B is a friend of class A.

Also, if you write friend class B in protected section of A, then it does NOT mean that B can access only protected and public members of A.

Always remember that once B becomes a friend of A, it can access any member of A, no matter in which section you write friend class B.

Nawaz
  • 353,942
  • 115
  • 666
  • 851
  • 4
    so I guess whoever told me that just didn't know what they were talking about. Thanks :) – BIU Jun 20 '11 at 06:54
2

c++ has the notion of 'hidden friends': http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p1601r0.pdf

Which only applies to friend functions that are defined inline. This make it so the functions can only be found via argument-dependent lookups, removing them from enclosing namespace.

Alfred Fuller
  • 773
  • 3
  • 5
-4

The friend declaration appears in a class body and grants a function or another class access to private and protected members of the class where the friend declaration appears.

As such access specifiers have no effect on the meaning of friend declarations (they can appear in private: or in public: sections, with no difference).

goyuiitv
  • 3
  • 3