I'm reading Accellerated C++ and they use a program for reading grades throughout the book, the code itself is a little confusing since there are a lot of what I beleive to be redundant function calls but there's one thing I don't understand. In one chapter we quantify all the functions and the student_info structure to a class, one function of which we use to read the info.
Code:
class Student_info {
public:
Student_info(); // construct an empty `Student_info' object
Student_info(std::istream&); // construct one by reading a stream
std::string name() const { return n; }
bool valid() const { return !homework.empty(); }
// as defined in 9.2.1/157, and changed to read into `n' instead of `name'
std::istream& read(std::istream&);
double grade() const; // as defined in 9.2.1/158
private:
std::string n;
double midterm, final;
std::vector<double> homework;
};
in the actual use of the program, it's used as
Code:
vector<Student_info> students;
Student_info record;
string::size_type maxlen = 0;
// read and store the data
while (record.read(cin)) { // changed
maxlen = max(maxlen, record.name().size()); // changed
students.push_back(record);
}
I uderstand that returns an istream because it is used in a while condition, but why a reference to an istream?
Some of the code is confusing in itself but what exactly is the benefit of returning a reference to an input stream?