On (08 Aug 97) Daniel Jones wrote to Jerry Coffin...
JC> DJ> Please sketch me a class definition for an I/O stream which
JC> DJ> doesn't use references and allows this:
JC> DJ> cout << "I have " << Number << " apples.";
JC> I don't think this is the example you intended you use - if you
JC> try
DJ> Well, it was the example I intended to use. Whether or not it proves
DJ> my point is another question.
Okay.
JC> do it, you'll find that while efficiency might be less than ideal
JC> (e.g., if `Number' is of some relatively large, complex type) but
JC> it's relatively easy and straightforward to code. However, doing
JC> input like this:
DJ> It is? Without references, your operator<< would have to return a
DJ> pointer to the ostream class.
Not so. It can return an instance of an ostream class constructed from
the original ostream object it was passed. For instance, you can grab
the file descriptor from the ostream, and construct a new ostream from
it. Returning the file descriptor (which is an int) is trivial.
Efficiency suffers considerably because you end up creating and
destroying ostreams at a rather high rate, but it's certainly possible.
DJ> I'm not aware of a way to concatenate
DJ> operators like this via a pointer to a class. Could you demonstrate?
No need for pointers. You can use actual objects...
JC> cout << "PLease enter the number of apples: " << flush;
JC> cin >> Number;
JC> is an entirely different story. Doing this without a reference is
JC> difficult indeed.
DJ> I'm really curious as to why this is easy and output is
DJ> straightforward. What am I missing here?
You're missing the fact that the input operator has to modify its
argument to do any good. If you had something like:
istream istream::operator(int x) {
// body
}
it'd be able to modify its copy of `x' on the stack, but couldn't do
_anything_ to the user's original object `x'.
Later,
Jerry.
... The Universe is a figment of its own imagination.
--- PPoint 1.90
---------------
* Origin: Point Pointedly Pointless (1:128/166.5)
|