title: What's additional advantage of using friend function in C++?
body: <p>I have a question with friend function in C++. I understand that if a function is defined as a friend function of a class, it can access any member variables or function regardless whether it is private, protected or public. Recently I am using doxygen to create document reference, I find another advantage of friend function: its relation with the class can be easily illustrated as friend function will be listed after the member function in the HTML page. However, if the function is not defined as friend, it will be regarded as a global function, and with doxygen it will not be listed with the class documentation. Then I plan to make all the global functions that have relationship with a specific class as its friend functions. I was wondering whether this is a good practice. </p>
  
  <p>The following codes show one function can be chosen as either as a friend function or a global function. </p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;map&gt;
  #include &lt;set&gt;
  #include &lt;iostream&gt;
  #include &lt;algorithm&gt;
  #include &lt;vector&gt;
  #include &lt;functional&gt;
  #include &lt;numeric&gt;
  #include &lt;string&gt;
  using namespace std;
  
  class ABCD
  {
  public: 
      int a;
      int b;
      friend void friend_fun(const ABCD &amp;obj);
  };
  
  void fun(const ABCD &amp;obj)
  {
      std::cout&lt;&lt;obj.a&lt;&lt;endl;
      std::cout&lt;&lt;obj.b&lt;&lt;endl;
  };
  
  void friend_fun(const ABCD &amp;obj)
  {
      std::cout&lt;&lt;obj.a&lt;&lt;endl;
      std::cout&lt;&lt;obj.b&lt;&lt;endl;
  };
  
  
  int main () 
  {
       ABCD obj;
       obj.a = 20;
       obj.b = 30;
       fun(obj);
       friend_fun(obj);
  
      return 0;
  }
  </code></pre>