From 503691726686060938
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f78e5,25d082aa6603a75e,start
X-Google-Attributes: gidf78e5,public
From: Tiaan Wessels <tiaan@inetsys.alt.za>
Subject: Streams Library
Date: 1995/11/07
Message-ID: <Pine.LNX.3.91.951107170313.3222A-100000@tiaan.inetsys.alt.za>#1/1
X-Deja-AN: 118861371
approved: fjh@cs.mu.oz.au
x-original-date: Tue, 7 Nov 1995 17:18:20 +0200 (GMT+0200)
organization: -
x-auth: PGPMoose V1.1 PGP comp.std.c++
newsgroups: comp.std.c++

[ 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. ]



