-3
istream& read_hw(istream& in, vector<double>& hw)
{
       if(in){
           hw.clear();

       double x;
       while (in >> x)
             hw.push_back(x);

       in.clear;
       }
       return in;
}

Can someone please explain this code to me. I have been reading the book Accelerated C++ and until now I have been loving it, but now that I am working with this new example I feel completely lost. This function is supposed to read homework grades from an input stream into a vector, but I am lost at a few places.

What is the variable type "istream" and why are we referencing it instead of just using it?

What is "if(in)" saying, does it mean if the input is successful execute the following code?

Also, I am having a hard time understanding why you need to use references a lot of the time, so if anyone can give me a little insight that would be really helpful to me.

I haven't been able to wrap my head around why you need to reference things if you aren't going to be changing their value at all, such as saying "const vector<(double)>& hw". I thought the whole point of a reference was to change the variable within the function for good.

Can anyone tell me what "return in;" will yield? Will it yield one of the arguments of the function or what?

Thank you so much for the help! It really helps to have somewhere to go as I am just learning C++. :)

  • _"and why are we referencing it instead of just using it?"_ What's the actual contradiction you seem to notice?? `std::istream ` et al don't work as values, since copying isn't possible (intentionally), so what's your question actually? Proving the fact?? – πάντα ῥεῖ Aug 11 '15 at 00:39
  • Why couldn't we just write istream read_hw instead of istream& – Lindsay Kolk Aug 11 '15 at 00:41
  • Streams can't be copied . It's a bit like saying "why would I give out my address instead of building people a copy of my house" – M.M Aug 11 '15 at 00:47
  • Because `read_hw` doesn't match the requirements of a `std::istream` perhaps? (remember copying isn't allowed or possible). – πάντα ῥεῖ Aug 11 '15 at 00:48
  • You need to read your book more carefully... – deviantfan Aug 11 '15 at 01:13

1 Answers1

1

What is the variable type "istream" and why are we referencing it instead of just using it?

The type is the generic std::istream, which provides uniform input operations reading from input sources (be it keyboard, file or string literals).

istream& read_hw(istream& in, vector<double>& hw) {
}

is intended to return the reference to the (possibly) modified in stream.

You should note, that std::istream cannot be copied, and you have to pass a reference through.

In case of globally overridden functions (like operator>>()), or specialized IO manipulators, this allows chaining these calls in a single expression statement.

What is if(in) saying, does it mean if the input is successful execute the following code?

It checks state of the in stream, will return false if a value couldn't be extracted successfully, or the stream reached the eof state.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190