getline() return value

hi guys I read from this

http://www.cplusplus.com/reference/string/string/getline/

and could not find the getline()'s return value I would have guessed that it is a bool but it doesn't seem to be,

so what is it's return type?

and if it's return type is a bool how come its possible to check against in a if statement or while loop?

1
2
3
4
  while (getline(firstName, surName))
 cout << firstName << " is your first name and your second is  " << surName << endl;
 }
1
2
3
4
if (condition) statement;
while (expression) statement;
do statement while (condition);
for (initialization; condition; increase) statement;
`condition' is a bool.
numbers may also be used, 0 is false, non zero is true
pointers may also be used, null is false, no-null is true


From http://www.cplusplus.com/reference/string/string/getline/
Return Value
The same as parameter is.
1
2
istream& //<--that's the return type
getline (istream&  is, string& str);

so you can check the input operation and also chain commands getline(input, line) >> n;
Now, ¿how that is interpreted as a bool?
http://www.cplusplus.com/reference/ios/ios/operator_bool/
1
2
operator void*() const; //c++98
explicit operator bool() const; //c++11 
the stream is casted to a pointer or to a bool and then is check.


By the way getline(firstName, surName), I doubt that your file is called `firstName'.
thanks for the reply ne,

that's quite confusion,I'm scratching my head,

how come it returns a reference of an ostream object I know you explained why but I'm still not too sure why,why not return a string or a bool to say if it was successful or not?


thanks
closed account (SECMoG1T)
this is why you need to return a reference and applies to most input output operations in c++, when overloading or in library definition.

https://stackoverflow.com/questions/28913009/why-we-need-to-return-reference-to-istream-ostream-while-overloading-and-o
Topic archived. No new replies allowed.