returning false as a stream?

This is a discussion on returning false as a stream? within the C++ Programming forums, part of the General Programming Boards category; hi. i'm trying to write a class with an overloaded >> operator, but i've run into a problem. when you ...

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    2

    returning false as a stream?

    hi. i'm trying to write a class with an overloaded >> operator, but i've run into a problem.

    when you use this operator with a stream and one of the base datatypes such as int, you can chain them. for example:

    Code:
    int intA, intB;
    ifstream inFile ( "bar.txt", ifstream::in );
    
    inFile >> intA >> intB;
    will set both intA and intB because
    Code:
    inFile >> intA
    returns a reference inFile.

    However, this also returns some manner of false value, as the following code illustrates:

    Code:
    int intA;
    ifstream inFile ( "bar.txt", ifstream::in );
    
    while (inFile >> intA) { cout << intA << endl; }
    this will continue reading until it can no longer retrieve anything from the stream, then the loop will break. My question is, how are these both accomplished in the same function? How can one return false as a stream reference?

    Any help would be appreciated. Thanks.

  2. #2
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,160
    It doesn't return false. It always returns a stream reference. The stream class has a type conversion operator which converts to a type which can be evaluated in a boolean context.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    Registered User
    Join Date
    Dec 2008
    Location
    Black River
    Posts
    128
    The operation:

    Code:
    stream >> variable;
    returns a stream by reference. The reason it can be used as a boolean expression is because it has overloaded the operator "void*". This means that when you do something like:

    Code:
    while(stream >> variable)
    It translates into:

    Code:
    while((void*)(stream >> variable))
    Operator void* returns NULL when the stream is in a "failed" state and any non-NULL value for every other case. Thus, it can be used in an "if", "while", etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. the chess project
    By Hussain Hani in forum Projects and Job Recruitment
    Replies: 8
    Last Post: 05-28-2007, 02:33 AM
  2. C++ FTP class won't work
    By lord mazdak in forum C++ Programming
    Replies: 8
    Last Post: 12-18-2005, 06:57 AM
  3. OpenGL Window
    By Morgul in forum Game Programming
    Replies: 1
    Last Post: 05-15-2005, 12:34 PM
  4. Wierd Segmentation Faults on Global Variable
    By cbranje in forum C Programming
    Replies: 6
    Last Post: 02-19-2005, 11:25 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21