Thread View: comp.std.c++
2 messages
2 total messages
Started by Martin Prange
Tue, 26 Sep 1995 15:13
operator(s) << are member functions. Why?
Author: Martin Prange
Date: Tue, 26 Sep 1995 15:13
Date: Tue, 26 Sep 1995 15:13
21 lines
1519 bytes
1519 bytes
To make the operator(s) << (from iostream.h) usable by other classes, they must be derived by the appropriate iostream-class. As non-member functions they could be used by any class, that could be casted to (ostream&) by means of e.g. operator ostream&. ( Which is, what I actually wanted to. ) So, the question is: Why are these operators declared (and implemented , of course), as members ? Are there design issues I`ve been missing so far? -- Martin Prange / Member of ALMAP Team / ALCATEL +49 030 3120 just alcanet: prange@sel.bln.alcatel.de else @cs.tu-berlin.de If you believe, the opinions expressed here are mine - you got it [ comp.std.c++ is moderated. Submission address: std-c++@ncar.ucar.edu. Contact address: std-c++-request@ncar.ucar.edu. The moderation policy is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
Re: operator(s) << are member functions. Why?
Author: Steve Clamage
Date: Tue, 26 Sep 1995 15:59
Date: Tue, 26 Sep 1995 15:59
33 lines
1928 bytes
1928 bytes
In article 95Sep26142017@slbwa7.bln.sel.alcatel.de, prange@slbwa7.bln.sel.alcatel.de (Martin Prange) writes:
>
>
>To make the operator(s) << (from iostream.h) usable by other classes,
>they must be derived by the appropriate iostream-class.
>
>As non-member functions they could be used by any class,
>that could be casted to (ostream&) by means of e.g. operator ostream&.
>( Which is, what I actually wanted to. )
>
>So, the question is:
>Why are these operators declared (and implemented , of course), as members ?
>Are there design issues I`ve been missing so far?
The design decision for iostreams was that the istream and ostream
conversion functions have a single definition. For that reason they were
made non-virtual member functions. When you use those conversion
functions inside your own I/O functions, such as
ostream& operator<<(ostream& o, const MyClass& m)
{ ... o << m.i ... }
you always know exactly what you will get.
Other designs are possible, but that was the reason. People differ on
whether that was the best possible choice.
---
Steve Clamage, stephen.clamage@eng.sun.com
[ comp.std.c++ is moderated. Submission address: std-c++@ncar.ucar.edu.
Contact address: std-c++-request@ncar.ucar.edu. The moderation policy
is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads