Modula2's godawful IO.

Glen Ditchfield gjditchfield at violet.waterloo.edu
Wed Apr 13 23:50:24 AEST 1988


In article <547 at m10ux.UUCP> rgr at m10ux.UUCP (Duke Robillard) writes:
>In article <96 at lzaz.ATT.COM> bds at lzaz.ATT.COM (BRUCE SZABLAK) writes:
>>I like C++'s overloading of the << and >> operator's even better than
>>printf. It allows you to define custom print routines for each structure
>>(class) declared, and then to print (to stdout for example) you do:
>>	cout << god_awful_structure_instance;
>
>Yeah, but that doesn't help me print out an error message, two strings,
>and an integer return code.  I don't want to have to write a method
>for every print....

Neither would I.  Fortunately for us, the i/o operators can be chained.
	cout << "Error: file " << fname << ", line " << lnum
	     << error_msg << "\n";
I think this is a big improvement over calls to overloaded "Print" procedures.

Details: "<<" associates left-to-right.  `cout << "Error: file "' returns
cout as it's value, which is used as the left operand by `<< fname'.  When
streams like cout (or the standard input, "cin") are used in an integer
context, the stream-to-int conversion returns the stream's status, so you
can write input loops like
	while ( cin >> val ) { ...
If a programmer-defined type must be read or written, the programmer writes
"<<" and ">>" operators for it.



More information about the Comp.lang.c mailing list