> operator of the istream class work? I tried to understand the statement: int main() { fstream inf( "ex...">

How does the "return value" of the >> operator of the istream class work?

I tried to understand the statement:

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

What returns ( inf >> c) in the while loop above? I downloaded the gcc source code and tried to play with it, but for me it was too complicated :(.

I checked the C ++ links page, I realized that it returns a link to itself:

    istream& operator>> (bool& val );
    istream& operator>> (short& val );
    istream& operator>> (unsigned short& val );
    istream& operator>> (int& val );
    istream& operator>> (unsigned int& val );
    istream& operator>> (long& val );
    istream& operator>> (unsigned long& val );
    istream& operator>> (float& val );
    istream& operator>> (double& val );
    istream& operator>> (long double& val );
    istream& operator>> (void*& val );

    istream& operator>> (streambuf* sb );

    istream& operator>> (istream& ( *pf )(istream&));
    istream& operator>> (ios& ( *pf )(ios&));
    istream& operator>> (ios_base& ( *pf )(ios_base&));

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

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

    istream& operator>> (istream& is, char* str );
    istream& operator>> (istream& is, signed char* str );
    istream& operator>> (istream& is, unsigned char* str );

So, I created a similar class, let's say my_istream:

struct my_istream {
    my_istream& self_ref;
};

int main() {
    my_istream mis;
}

when compiling, I got this error:

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

, , self_ref ? , , (&) ++ - C. ? istream? ? !

EDIT:

struct my_istream {
    my_istream() {
    }

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

int main() {
    my_istream mis;
    int x;
    while( mis.operator>>( x ) ) {
        cout << "--";
    }
}

my_istream while?

+3
2

self, return *this. . , : class Foo { Foo(int& some_int_ref) :my_ref_member(some_int_ref)

istream .

(, operator >>, ) ( , ):

#include <iostream>

class X {
    bool B;
public:
    X() :B(false) { }
    X& toggle() { B = !B; return *this; }
    operator void*() { return B ? this : 0; }
};

int main()
{
    X x;

    x.toggle().toggle().toggle();
    if (x)
        std::cout << "true!" << std::endl;
}

EDIT: operator bool vs operator void * , stackoverflow : "BOOL()" , "long" ,

+2

(inf → c) while ?

void*. while (inf.operator void*() != NULL).

?

bool - bool.

istream?

(return *this), .

+1

Source: https://habr.com/ru/post/1782151/


All Articles