FAQ 19.14 How are objects of a class printed?

Objects of a class are normally printed via a friend function called operator<<. Here is an example of such a friend function.

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

The function operator<< is a friend rather than a member, so the Fred parameter appears on the right side of the <<.



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