title: Using ofstream* wrapper class with overloaded << operator on endl
body: <p><strong>C++</strong></p>
  
  <p>This is an attempt to make a class that mimics the output behavior of using the <code>&lt;&lt;</code> operator of an <code>ofstream</code>, as far as being able to use <code>std::endl</code> and write <code>string</code>s is concerned. The class has a single data member, the <code>ofstream</code> pointer. The class has two overloaded <code>&lt;&lt;</code> operators, one that takes an <code>std::string</code> and another that takes a pointer to a function, whose argument is an <code>ostream</code> reference and returns an <code>ostream</code> reference. That is the signature of <code>std::endl</code>, according to <a href="http://www.cplusplus.com/reference/ostream/endl/?kw=endl" rel="nofollow">this</a>. Technically, the below program works with the given input. It is able to print to file, two lines of text separated by two <code>std::endl</code>s. However, I want my <strong>non</strong>-string parameter overloaded <code>&lt;&lt;</code> operator to accept <code>std::endl</code> only, not something that merely matches its signature. I tried various combinations of placing <code>std::endl</code>in the argument list, with and without <code>*</code> and with and without <code>&amp;</code>, but I got compiler errors for every combination. C++11 answers are also welcome.</p>
  
  <pre><code>#include &lt;fstream&gt;
  #include &lt;iostream&gt;
  #include &lt;string&gt;
  
  class TextOut
  {
  public:
      TextOut(std::ofstream* ofsPar) : ofs(ofsPar) {}
      TextOut&amp; operator&lt;&lt;(std::string s)
      {
          *ofs &lt;&lt; s;
          return *this;
      }
      TextOut&amp; operator&lt;&lt;(std::ostream&amp; (*endlPar) (std::ostream&amp; os))
      {
          *ofs &lt;&lt; std::endl;
          return *this;
      }
  private:
      std::ofstream* ofs;
  };
  
  int main()
  {
      std::cout &lt;&lt; "Enter filename: ";
      std::string filename;
      std::cin &gt;&gt; filename;
      std::ofstream myofstream(filename.c_str());
      TextOut myTextOut(&amp;myofstream);
      myTextOut &lt;&lt; "Hello," &lt;&lt; std::endl &lt;&lt; std::endl &lt;&lt; "spacious world.";
      return 0;
  }
  </code></pre>
  
  <p><strong>Output:</strong></p>
  
  <pre><code>Hello,
  
  spacious world.
  </code></pre>