@mbozzi I'm step-debugging through my own code example at the moment, which is essentially the same as the code example you'd provided, and have noticed a few things:
1. When
istream_iterator<Point> in
is created and bound to a stream (Line 45), a "dummy"
Point
object is default-initialized and the
in
iterator refers/points to this "dummy" object.
2. It looks like the program uses this "dummy" Point object as a "working" variable. When the stream is read from i.e., when you advance the iterator with operator++, the body of
operator>>
is invoked. Rather than creating a new Point object each time the iterator is advanced, the operator>> reuses this dummy variable.
As the class author, what behavior are you suppose to specify in the body of
operator>>
?
Apparently, reading from the stream inside operator>> is optional - you can choose to do nothing inside the body of
operator>>
! But the fact that you're given a reference to a stream and the same dummy variable seem to hint that the body of operator>> is where you should specify what to do with the data that is read in from the stream to initialize a Point object. In other words, you're implementing what it means to "read a Point object from the stream bound to the istream_iterator".
3. You'd written:
mbozzi wrote: |
---|
Typically, the string representation of the thing being streamed doesn't depend on the stream. For example, the textual representation of the integer forty-two is usually the same "42" no matter whether the stream is connected to a network, file, or standard I/O. That means dynamic_cast is usually unnecessary.
|
Is it possible for
istream
to contain multiple types e.g., int, double, string, or perhaps custom user-types?
I'm assuming that a stream can. If so, then presumably, the thing (could be a thread, or another application) writing to the stream and the thing reading from the stream has agreed on the format of the stream and what should happen when the other writes to or reads from the stream (e.g., "The stream shall have this format: a repeating sequence of a Foo object, a double, a string, and then the special sequence of 3 chars, '\0\0\0'"). This kind of specification would be necessary for the person implementing operator>>.
4. Can I specify specific input stream types instead of
istream& operator>>(istream&, ...)
to specify what should happen when Point is read from a file stream, vs. a string stream, vs. a network?