title: Why do we need passing by reference when overloading ">>" and "<<"?
body: <p>I didn't understand very well why we need reference before the istreams and ostreams (in all 4 of them in my code). I've kind of understood why the ones that are function arguments need ampersant, because the copy constructors is not allowed to act on ostream and istream, so they need reference, but why do the ones that specify the type that is returned? I really can not figure that out, and moreover, i can't understand why the code doesn't even compile if I remove the ampersants from them ( I get this error: <em>error: ambiguous overload for 'operator&lt;&lt;' (operand types are 'std::ostream' {aka 'std::basic_ostream'} and 'foo')</em>).</p>
  <pre><code>
  #include &lt;iostream&gt;
  using namespace std;
  
  class foo
  {
      int x;
  
   public:
      foo()
      {
          x=10;
      }
   
      friend istream&amp; operator&gt;&gt;(istream&amp; in, foo&amp; S);
      friend ostream&amp; operator&lt;&lt;(ostream&amp; out, foo&amp; S);
      
  };
  
  istream&amp; operator&gt;&gt;(istream&amp; in, foo&amp; S)
      {
          in&gt;&gt;S.x;
          return in; 
      }
  
  ostream&amp; operator&lt;&lt;(ostream&amp; out, foo&amp; S)
      {
          out&lt;&lt;S.x;
          return out;
      }
  
  int main()
  {
  
      foo A, B, C;
  
      cin&gt;&gt;A;
  
      cout&lt;&lt;A;
  
      return 0;
  }
  </code></pre>