Article View: comp.std.c++
Article #2048Re: operator(s) << are member functions. Why?
From: 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. ]
Technical Details
Message-ID:
<4496na$ppa@engnews2.Eng.Sun.COM>Path:
arc1-ca.pugleaf.net!engnews2.Eng.Sun.COM!taumet!clamageReferences:
<PRANGE.95Sep26142017@slbwa7.bln.sel.alcatel.de>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!engnews2.Eng.Sun.COM!taumet!clamage From: clamage@Eng (Steve Clamage) Newsgroups: comp.std.c++ Subject: Re: operator(s) << are member functions. Why? Date: 26 Sep 1995 15:59:14 GMT Organization: Sun Microsystems Inc. Approved: clamage@eng.sun.com (comp.std.c++) Message-ID: <4496na$ppa@engnews2.Eng.Sun.COM> References: <PRANGE.95Sep26142017@slbwa7.bln.sel.alcatel.de> Reply-To: clamage@Eng NNTP-Posting-Host: taumet.eng.sun.com Content-Type: text X-Nntp-Posting-Host: taumet.eng.sun.com Content-Length: 1105 X-Lines: 29 Originator: clamage@taumet