Thread: reterning a reference to the input stream

  1. #1
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901

    Question reterning a reference to the input stream

    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?

  2. #2
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    istreams cannot be copied. That's the reason. DUH!
    Last edited by Mario F.; 06-22-2006 at 10:29 AM.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  3. #3
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    you should add "DUUUUH!" at the end. I should have read that.

    ...that was rather anti-climactic.

  4. #4
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Edited
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can someone help me with these errors please code included
    By geekrockergal in forum C Programming
    Replies: 7
    Last Post: 02-10-2009, 02:20 PM
  2. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  3. Question about OpenGL/Linux
    By Ideswa in forum Linux Programming
    Replies: 12
    Last Post: 09-10-2006, 05:56 AM
  4. How to: Use OpenGL with Jgrasp
    By Pickels in forum Game Programming
    Replies: 3
    Last Post: 08-30-2005, 10:37 AM
  5. Buffering??
    By Brain Cell in forum C Programming
    Replies: 15
    Last Post: 09-21-2004, 06:57 AM