operator >> return value

This is a discussion on operator >> return value within the C++ Programming forums, part of the General Programming Boards category; I'm a bit confused about what the expression belove in the code returns. Code: cin >> buf; And how can ...

  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
    22,258
    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).
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,991
    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, 08:17 AM
  2. Another weird error
    By rwmarsh in forum Game Programming
    Replies: 4
    Last Post: 09-24-2006, 11: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, 02: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

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