leo@atcmp.nl (Leo Willems) (05/25/89)
In: int i; while ( cin >> i) //... the expression ``cin >> i'' gives as result an istream&. That's the part I understand. This istream& (which represents an istream object) is converted using ``operator istream*() { return _eof < state ? 0 : this; }'' to give a value which can be used in a while expression My question is: Why does this conversion take place? There is no assignment involved (i.e. to a void*) from which the type system can decide which conversion is appropriate. The only explanation I can give is that class istream has only one conversion operator so there isn't much choise. 1) if my explanation is incorrect, then where did I go wrong 2) correct or not: the returning ``this'' from the conversion operator is a pointer, not a reference. Does is get converted somehow to a reference? Leo Willems Internet: leo@atcmp.nl AT Computing UUCP: mcvax!hp4nl!kunivv1!atcmpe!leo P. O. Box 1428 6501 BK Nijmegen The Netherlands
jss@hector.UUCP (Jerry Schwarz) (05/26/89)
In article <522@atcmpe.atcmp.nl> leo@atcmp.nl (Leo Willems) considers code that looks like while ( cin >> i) ... The istream& returned by the extraction operator(>>) gets converted to a void* by operator void*::istream. > >My question is: Why does this conversion take place? There is no >assignment involved (i.e. to a void*) from which the type system can decide >which conversion is appropriate. In C++ conversions can arise implicitly in many contexts other than the right hand side of assignment. In particular in "boolean context", if the type of the expression is not one that can be tested against zero (arithmetic or pointer type) then C++ tries to convert it with a user defined conversion operator. If there is more than one such conversion operator that converts to a type that can be tested against 0, then there is an ambiguity. Boolean contexts are (unless I've overlooked something): expression in an if statement expression in a while statement expression in a repeat until statement 2nd expression in a for statement either operand of logical operators (&&, ||) Jerry Schwarz AT&T Bell Labs, Murray Hill