How does the "return value" of operator >> of istream class work?
<p>I tried to understand the statement:</p>

<pre><code>    int main() {
        fstream inf( "ex.txt", ios::in );
        char c;
        while( inf &gt;&gt; c ) {
            cout &lt;&lt; c &lt;&lt; ", ";
        }
        return 0;
    }
</code></pre>

<p>What does ( <code>inf &gt;&gt; c</code> ) return in the while loop above? I downloaded the gcc source code and tried to play around with it but it was way too complex for me :( .</p>

<p>I checked the C++ reference page, I realized that it return a reference to itself:</p>

<pre><code>    istream&amp; operator&gt;&gt; (bool&amp; val );
    istream&amp; operator&gt;&gt; (short&amp; val );
    istream&amp; operator&gt;&gt; (unsigned short&amp; val );
    istream&amp; operator&gt;&gt; (int&amp; val );
    istream&amp; operator&gt;&gt; (unsigned int&amp; val );
    istream&amp; operator&gt;&gt; (long&amp; val );
    istream&amp; operator&gt;&gt; (unsigned long&amp; val );
    istream&amp; operator&gt;&gt; (float&amp; val );
    istream&amp; operator&gt;&gt; (double&amp; val );
    istream&amp; operator&gt;&gt; (long double&amp; val );
    istream&amp; operator&gt;&gt; (void*&amp; val );

    istream&amp; operator&gt;&gt; (streambuf* sb );

    istream&amp; operator&gt;&gt; (istream&amp; ( *pf )(istream&amp;));
    istream&amp; operator&gt;&gt; (ios&amp; ( *pf )(ios&amp;));
    istream&amp; operator&gt;&gt; (ios_base&amp; ( *pf )(ios_base&amp;));

    *** the following functions are not members but GLOBAL functions:

    istream&amp; operator&gt;&gt; (istream&amp; is, char&amp; ch );
    istream&amp; operator&gt;&gt; (istream&amp; is, signed char&amp; ch );
    istream&amp; operator&gt;&gt; (istream&amp; is, unsigned char&amp; ch );

    istream&amp; operator&gt;&gt; (istream&amp; is, char* str );
    istream&amp; operator&gt;&gt; (istream&amp; is, signed char* str );
    istream&amp; operator&gt;&gt; (istream&amp; is, unsigned char* str );
</code></pre>

<p>So I created a similar class, let's say my_istream:</p>

<pre><code>struct my_istream {
    my_istream&amp; self_ref;
};

int main() {
    my_istream mis;
}
</code></pre>

<p>when compiling, I got this error:</p>

<pre>
1>c:\users\chan\documents\visual studio 2010\projects\topcoder\topcoder\main.cpp(26): error C2758: 'my_istream::self_ref' : must be initialized in constructor base/member initializer list
</pre>

<p>However, I don't really know what should self_ref be initialized to in this case? I understand a pointer to itself when dealing with linked-list, and I understand that reference( <code>&amp;</code> ) in C++ is just a disguised form of pointer in C. But I can't explain this situation? How does the internal implementation of <code>istream</code> actually work? How does a reference can be evaluated as true or false? Thanks!</p>

<p><strong>EDIT:</strong></p>

<pre><code>struct my_istream {
    my_istream() {
    }

    my_istream&amp; operator &gt;&gt;( int x ) {
        return *this;
    }
};

int main() {
    my_istream mis;
    int x;
    while( mis.operator&gt;&gt;( x ) ) {
        cout &lt;&lt; "--";
    }
}
</code></pre>

<p>What should I add to my_istream class in order to work inside the while loop?</p>
