title: Do C++ private functions really need to be in the header file?
body: <p>I have always thought of header files as a sort of 'public interface' describing a class, in which case it would be better to keep private fields and functions in the .cpp file.</p>
  <p>I understand that private fields need to be in the header so that other classes can tell how much memory an instance of a class will consume, but it occurred to me as I was about to write a private helper function, that this function could be made static, in which case there was no need for it to be 'part of the class' at all, it could just as easily be a regular function in the class definition's .cpp file.</p>
  <p>It then occurred to me that <em><strong>all</strong></em> private functions could potentially be rewritten to be static by accepting pointers/references to class fields instead of expecting to be defined in the class.</p>
  <p>This would eliminate the need to declare any private functions in the header file.</p>
  <p>I do like to follow conventions, so is it considered an established convention in C++, that non-static private functions <em><strong>should</strong></em> be in the header file? What about static functions or static constants?</p>
  <p>I'm going to put in some code to explain what I'm getting at:</p>
  <p><strong>.h file:</strong></p>
  <pre><code>#ifndef SOME_CLASS_H
  #define SOME_CLASS_H
  
  class SomeClass
  {
  private:
      int x;
  public:
      void combineWithX(int y);
  };
  
  #endif
  </code></pre>
  <p><strong>.cpp file</strong></p>
  <pre><code>#include &quot;SomeClass.h&quot;
  
  void someHelper(int* x)
  {
      *x = (*x) + 1;
  }
  
  void SomeClass::combineWithX(int y)
  {
      someHelper(&amp;x);
      x += y;
  }
  </code></pre>
  <p>Note that <code>someHelper(int* x)</code> in the .cpp file references the private member x in spirit, but not directly, and therefore does not need to appear in the header. I'm wondering if this sort of thing is considered 'bad style'.</p>