Thread: operator >> return value

  1. #1
    Registered User
    Join Date
    Jul 2009
    Location
    Croatia
    Posts
    272

    operator >> return value

    I'm a bit confused about what the expression belove in the code returns.

    Code:
    cin >> buf;
    And how can it be used in a while loop:
    Code:
    string buf;
    while(cin >> buf) //extracts until eof
       ;

    I checked cpp reference:

    Code:
    Return Value
    The object itself (*this).
    When a value is being "extracted" it is not returned, but directly stored in the variable used as parameter.
    It doesn't say anywhere that the return value is zero in case of EOF.

    Does the "cin" object in our case becomes something like a NULL and then the while terminates?

    I'm confused about this pointer aswell...

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Tool
    Does the "cin" object in our case becomes something like a NULL and then the while terminates?
    Yes. The result, which is a reference to std::istream, is converted to void*. The void pointer is a null pointer when the stream is no longer in a good state (if I remember correctly).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The stream operator will always return a reference to itself (ie *this).
    What it doesn't say is that the class has an overloaded operator void* which is invoked in your comparison.
    Ie -> operator >> is called, a std:stream is returned, std:stream's void* operator is called -> expression is converted to true/false (this operator is invoked on the object that is returned from the >> operator, but since it's the same object that you used the >> operator on in the first place...).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A basic optimization request...
    By feeder74 in forum C++ Programming
    Replies: 27
    Last Post: 05-05-2010, 07:17 AM
  2. Another weird error
    By rwmarsh in forum Game Programming
    Replies: 4
    Last Post: 09-24-2006, 10:00 PM
  3. Why only 32x32? (OpenGL) [Please help]
    By Queatrix in forum Game Programming
    Replies: 2
    Last Post: 01-23-2006, 02:39 PM
  4. opengl help
    By heat511 in forum Game Programming
    Replies: 4
    Last Post: 04-05-2004, 01:08 AM
  5. Algorithm to walk through a maze.
    By Nutshell in forum C Programming
    Replies: 30
    Last Post: 01-21-2002, 01:54 AM