title: C++ return reference variable in prefix overloading
body: <p>I am confused when all the code I find shows returning a reference variable when overloading the prefix operator. I went through the parashift.com FAQ (http://www.parashift.com/c++-faq-lite/operator-overloading.html#faq-13.14) and it isn't clear, even though it says it WILL be clear when you read it. I adapted their example into a nonsensical, pointless little program to test.</p>
  
  <pre><code>#include&lt;iostream&gt;
  using namespace std;
  
  class Number {
     public:
        Number&amp; operator++ ();    // prefix ++
        Number  operator++ (int); // postfix ++
        int value() { return value_; }
        void setValue(int value) { value_ = value; }
     private:
        int value_;
  };
  
  Number&amp; Number::operator++ () {
     ++value_;
     return *this;
  }
  
  Number Number::operator++ (int unused) {
     Number temp;
     temp.setValue(value_);
     ++value_;
     return temp;
  }
  
  int main()
  {
     Number someNum;
     someNum.setValue(20);
     cout &lt;&lt; "someNum : " &lt;&lt; someNum.value() &lt;&lt; "\n";
     someNum++;
     ++someNum;
     cout &lt;&lt; "someNum : " &lt;&lt; someNum.value() &lt;&lt; "\n";
     return 0;
  }
  </code></pre>
  
  <p>The problem is, it works if I just declare it as a Number object as well like so:</p>
  
  <pre><code>#include&lt;iostream&gt;
  using namespace std;
  
  class Number {
     public:
        Number operator++ ();    // prefix ++
        Number  operator++ (int); // postfix ++
        int value() { return value_; }
        void setValue(int value) { value_ = value; }
     private:
        int value_;
  };
  
  Number Number::operator++ () {
     ++value_;
     return *this;
  }
  
  Number Number::operator++ (int unused) {
     Number temp;
     temp.setValue(value_);
     ++value_;
     return temp;
  }
  
  int main()
  {
     Number someNum;
     someNum.setValue(20);
     cout &lt;&lt; "someNum : " &lt;&lt; someNum.value() &lt;&lt; "\n";
     someNum++;
     ++someNum;
     cout &lt;&lt; "someNum : " &lt;&lt; someNum.value() &lt;&lt; "\n";
     return 0;
  }
  </code></pre>
  
  <p>I assume I simply need a better understanding of reference variables. Can anyone explain simply why the prefix operator SHOULD be coded as returning a reference variable?</p>