Article View: comp.std.c++
Article #2466Streams Library
From: Tiaan Wessels
Date: Tue, 07 Nov 1995 15:48
Date: Tue, 07 Nov 1995 15:48
81 lines
3312 bytes
3312 bytes
[ Moderator's note:
This post is an incarnation of the Frequently Asked Question
"Why aren't the iostream operators << and >> declared virtual?"
It would be great if someone would write a clear, concise,
and coherent answer to this question for inclusion in the FAQ list.
- moderator (fjh).
]
I have a question regarding the streams library's << and >> operators for
the standard C types. I want to write a class derived from i/ostream and
I want to overload all the operators and member functions. The reason I
want to do this is because my stream will be a network stream and I want
to convert the relevant types to network byte order by calling the
network library's functions inside the ostream& << ( int ) and whatever
member functions of i/ostream wherever byte order will cause a problem.
Furthermore I want to make my own structs streamable in my program by
writing standalone functions taking a i/ostream and my struct as
parameter and streaming it as I want to. The i/ostream parameter is
passed by reference by using the & designator causing dynamic binding
should a derived class of iostream be passed in the place of a standard
stream such as fstream. The operators however were not declared virtual
making what I want to do impossible. Is there any specific reason for
those operators not being virtual ? I don't want to go and redefine
stream operators for my structs for both standard C++ streams and my own
custom streams seperately. If what I'm saying is not clear from this text
I include a small example
class TLIostream : public ostream
{
public:
.
.
ostream& operator<<(const char *s) { // write string out on network }
ostream& operator<<(int n) { // convert n to network byte order and
// write out on tli stream }
.
.
};
struct City
{
char *Name;
int Population;
};
ostream &operator<<( ostream &stream, City c )
{
stream << c.Population << "\t" << c.Name << "\n";
return stream;
}
void main()
{
City c;
ofstream f( "test.file" );
TLIostream t( "192.96.51.58" );
c.Name = new char[20];
strcpy( c.Name, "Downunder" );
c.Population = 200;
f.open( "test.file" );
f << c;
t << c;
f.close();
t.close();
delete c.Name;
}
I would be glad if this was possible however, if I'm not totally of
track, the fact that the operators was not declared virtual in the first
place, makes this impossible.
Tiaan Wessels
tiaan@inetsys.alt.za
Netsys International
---
[ 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:
<Pine.LNX.3.91.951107170313.3222A-100000@tiaan.inetsys.alt.za>Path:
arc1-ca.pugleaf.net!in2.uu.net!bounce-backShow Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!in2.uu.net!bounce-back Date: 07 Nov 95 15:48:21 GMT Approved: fjh@cs.mu.oz.au Organization: - Newsgroups: comp.std.c++ X-Original-Date: Tue, 7 Nov 1995 17:18:20 +0200 (GMT+0200) From: Tiaan Wessels <tiaan@inetsys.alt.za> Subject: Streams Library Message-ID: <Pine.LNX.3.91.951107170313.3222A-100000@tiaan.inetsys.alt.za> X-Auth: PGPMoose V1.1 PGP comp.std.c++ iQBFAgUBMJ+BPuEDnX0m9pzZAQHKcwF8DWdYQCEY7X04lw8WNHHvlo6tPpRwiemz OiPs/6Pir21xjxl8nzUOejIE8rd8NUMU =ojL/