>
std::istream& /* Is this is a reference to the incoming stream, or an inheritance of istream? */
T&
is reference to
T;
std::istream&
is reference to
std::istream. We can pass a reference to an object of a derived class (say, a
std::ifstream) - there is an implicit conversion.
In effect, the function can be called with a reference to any input stream object; and it returns a reference to the same stream object.
> Whenever ">>" is used immediately after "stm", this function is called.
Yes. we have overloaded the
>> operator for
a >> b
where
a
is an input stream and
b
is a Page.
It is now easy to see why the operator returns a reference to the stream: we can chain input operations a la
std::cin >> i >> j ;
. First
std::cin >> i
is evaluated; the result is a reference to
std::cin
,
and on that reference
reference >> j
(
std::cin >> j
) is evaluated.
> I get a "error: range-based 'for' loops are not allowed in C++98 mode".
If you get that error, the compiler is clearly aware of C++11.
Why don't you want to compile it in C++11 mode, with the option
-std=c++11?
> how do I write this to be backwards compatible with C++98?
As a classical for loop:
1 2 3
|
// for( int& i : p.reqSt ) stm >> i ;
const int SZ = 4 ;
for( int i = 0 ; i < SZ ; ++i ) stm >> p.reqSt[i] ;
|
See:
http://www.stroustrup.com/C++11FAQ.html#for
>
if( !stm ) p = {} ; // if stm fails/does not exist, wipe all fields
That was the intent. (It doesn't really wipe the arrays). That too uses a C++11 construct: uniform initialization.
http://www.stroustrup.com/C++11FAQ.html#uniform-init
In C++98, we would have to write it the longer way.
> After building all fields within p, using stm as the input, return p
> so that we can use the fields in the caller... right?
No. The
Page p
is passed by reference. We don't have to return it; we have modified the the caller's object.
The function returns
stm
, the reference to the input stream.