FAQ 19.15 How do objects of a class receive stream input?

Objects of a class normally receive stream input via a friend function called operator>>. Here is an example of such a friend function.

 #include <iostream> using namespace std; class Fred { public:   friend istream& operator>> (istream& istr, Fred& x) throw(); protected:   int i_; }; istream& operator>> (istream& istr, Fred& x) throw() {   istr >> x.i_;   return istr; } 

The Fred argument of operator>> is passed by reference (as opposed to const reference). This allows operator>> to change the caller's Fred, which is, of course, the whole point of stream input.



C++ FAQs
C Programming FAQs: Frequently Asked Questions
ISBN: 0201845199
EAN: 2147483647
Year: 2005
Pages: 566
Authors: Steve Summit

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net