reterning a reference to the input stream

This is a discussion on reterning a reference to the input stream within the C++ Programming forums, part of the General Programming Boards category; I'm reading Accellerated C++ and they use a program for reading grades throughout the book, the code itself is a ...

  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
    Portugal
    Posts
    7,412
    istreams cannot be copied. That's the reason. DUH!
    Last edited by Mario F.; 06-22-2006 at 10:29 AM.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    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
    Portugal
    Posts
    7,412
    Edited
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    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, 01: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21