Modula2's godawful IO.

Dave Jones djones at megatest.UUCP
Tue Apr 19 11:01:10 AEST 1988


in article <553 at m10ux.UUCP>, rgr at m10ux.UUCP (Duke Robillard) says:

...

  [ cout << foo << bar; // etc. ]

> 
>     However, I don't really see that this is any different than 
> overloading a print procedure.  all you're doing is overloading "<<"
> (i.e. making it handle strings, integers, reals, whatever), right?
> 

That's right.  It's just easier to type and to read than having a
jillion "print"'s.

For better or worse (*better* in my opinion), C++ classes don't tend
to "know how to print themselves".  Instead you overload the << operator
to print new classes.   Also remember that in C++, "int" is not a class!!  
So the function can't be a member of class int. Translated into 
Smalltalk, the class "int" does not recognize the message "<<".

"Print an int" _could_ be an external function, defined this way:

   ostream& operator<< (ostream& o, int a) {return o << (long)a;}

There is an argument to be made that it SHOULD be declared this way,
to reduce the number of member functions of ostream. That is to say,
to reduce the number of types which ostream "knows about".

But the way things are actually implemented, the function is a member of 
ostream, and has this declaration:

    ostream& operator<<(int a) { return *this<<long(a); }



More information about the Comp.lang.c mailing list