Topic: IOStreams (NON-virtual) operator (s) <<


Author: shepherd@debussy.sbi.com (Marc Shepherd)
Date: 1995/05/11
Raw View
In article 3qi@engnews2.Eng.Sun.COM, clamage@Eng.Sun.COM (Steve Clamage) writes:
>In article opu@lyman.pppl.gov, Haim Kreitman <kreitman@mhs.scitex.com> writes:
>>I want to write an ostream class which will NOT format the fundemental types.
>>That is,
>>
>>void foo(ostream& os) {
>> os << 1L;
>> }
>>
>>will ( when calling foo as "foo(myostream)" ) output the 1L in it's binary
>>representation.
>
>By definition, the iostream overloaded << and >> operators do text formatting.
>
>If you want straight binary I/O, use the read/write functions, which
>by definition do no formatting. Example:
>
>void foo(ostream& os) {
> long l = 1;
> os.write(&l, sizeof(l)); // might need to cast &l to char*
> }
>

You can also write your own class (derived from the relevant iostream class(es)),
and overload operator<< as you please.  For example, you could call your class
'bin_ostream'.  If you expect to be doing a lot of binary I/O, mixing a lot of
different types, this could be worthwhile.










---
Marc Shepherd
Salomon Brothers Inc
shepherd@schubert.sbi.com The opinions I express are no one's but mine!






Author: fjh@munta.cs.mu.OZ.AU (Fergus Henderson)
Date: 1995/05/05
Raw View
Haim Kreitman <kreitman@mhs.scitex.com> writes:

>I want to write an ostream class which will NOT format the fundemental types.

But `ostream' is for formatted output!
If you want unformatted output, use a different class.

>void foo(ostream& os) {
> os << 1L;
> }
>
>will ( when calling foo as "foo(myostream)" ) output the 1L in it's binary
>representation.
>
>So, my question is why aren't ostream's operators << virtual ( so I can
>easily override them ... ).

So that people aren't tempted to do silly things like overriding them.
(You would immediately run into a classic multi-methods problem -
if there are N different stream classes, and M different types,
you would need N*M different methods.)

(The above is all IMHO, and should probably be taken with a grain of salt.)

--
Fergus Henderson                       | I'll forgive even GNU emacs as
fjh@cs.mu.oz.au                        | long as gcc is available ;-)
http://www.cs.mu.oz.au/~fjh            |             - Linus Torvalds





Author: clamage@Eng.Sun.COM (Steve Clamage)
Date: 1995/05/02
Raw View
In article opu@lyman.pppl.gov, Haim Kreitman <kreitman@mhs.scitex.com> writes:
>hi all,
>
>This is a strange behavior of the iostream classes. I don't know the current state
>of the standard, so if anyone can clear this issue for me ...
>
>I want to write an ostream class which will NOT format the fundemental types.
>
>That is,
>
>void foo(ostream& os) {
> os << 1L;
> }
>
>will ( when calling foo as "foo(myostream)" ) output the 1L in it's binary
>representation.

By definition, the iostream overloaded << and >> operators do text formatting.

If you want straight binary I/O, use the read/write functions, which
by definition do no formatting. Example:

void foo(ostream& os) {
 long l = 1;
 os.write(&l, sizeof(l)); // might need to cast &l to char*
 }

If some of your overloaded << and >> operators do text formatting and
some do not, the result will be confusing and hard to use. Similarly,
if you give nonstandard meanings to standard operations, (e.g., iostream <<
and >> operators which do no formatting is the opposite of the standard)
the result will be confusing and hard to use.

>So, my question is why aren't ostream's operators << virtual ( so I can
>easily override them ... ).

One possible design would be to make these functions virtual
so that you could provide custom behavior in derived classes.

Another possible design is to make these functions non-virtual so that
you always know what behavior you will get when you invoke the functions.

Both choices have advantages and disadvantages. The original design choice
for iostreams was non-virtual formatting functions to provide known behavior.
We discussed this issue in the C++ Committee and decided not to change it.
---
Steve Clamage, stephen.clamage@eng.sun.com







Author: Haim Kreitman <kreitman@mhs.scitex.com>
Date: 1995/05/01
Raw View
hi all,

This is a strange behavior of the iostream classes. I don't know the current state
of the standard, so if anyone can clear this issue for me ...

I want to write an ostream class which will NOT format the fundemental types.

That is,

void foo(ostream& os) {
 os << 1L;
 }

will ( when calling foo as "foo(myostream)" ) output the 1L in it's binary
representation.

So, my question is why aren't ostream's operators << virtual ( so I can
easily override them ... ).

And, if anyone knows how to bypass this behavior without changing headers etc.

Thanks,
Haim Kreitman.