Input and Output Operators V

 [Previous Chapter]  [Previous Page]  [Contents]  [Next Page]  [Next Chapter]

QuotedString.C
ostream& operator<<(ostream& out, const QuotedString& qs) {
   return out << '"' << qs.get() << '"';
} // operator<<
Contact.C
ostream& operator<<(ostream& out, const Contact& contact) {
   return out << '(' <<
      QuotedString(contact.get_name()) <<
      ", " <<
      QuotedString(contact.get_address()) <<
      ')';
} // operator<<

*Output formats should usually conform to their corresponding input formats.
 
*Note that this example fails in this aspect if the name or the address of a contact contains quotes. This could be fixed by extending the QuotedString format with escape sequences.
 

 [Previous Chapter]  [Previous Page]  [Contents]  [Next Page]  [Next Chapter]
Copyright © 2001, 2002 Andreas Borchert, converted to HTML on February 21, 2002