[CODE] Operator overloading (C++)

From: Brian Gray (mandy@NETCOM.COM)
Date: 08/18/97

Howdy all.  I just resubscribed about 5 minutes ago after about a 2-year
break.  Did I miss anything?

Well to jump right in, anyone out there any good with C++?  I've
converted a Circle-based game to C++ just as far as getting it to compile
with g++.  In other words, I've not yet redesigned the architecture to
use proper classes and object-orientedness.

Anyhoo, I thought it would be swell to overload the operator<< for
char_data in effect to replace send_to_char.  I did this in the form of a
template which follows:

#include <strstream.h>

template <class T> char_data * operator<<(char_data *ch, T msg) {
  ostrstream strout;              // inherit ostream to get inserters
  strout << msg << ends;          // insert message and NULL-terminate
  send_to_char(strout.str(), ch); // lock char* and send it
  strout.freeze(0);               // unlock char* so class can clean up
  return ch;

Unfortunately, the compiler bitches about the fact that char_data is a
struct rather than a class.  Is there any way around this other than
actually changing it into a class?  Could I get away with just replacing
"struct" with "class" in structs.h and adding the line "public:" at the
top?  If I go that way, I guess I might as well define
char_data::send().  Obviously, I eventually want to protect all data
members and provide access functions, but first things first.  For now,
I'd be happy ditching those sprintf(buf, ...)'s and coding:

ch << "Brought to you by the numbers " << 4.7 << " and " << 47 << "\n\r"
   << "and the letter " << 'B' << "\n\r";

 -- Brian

     | Ensure that you have read the CircleMUD Mailing List FAQ:  |
     | http://democracy.queensu.ca/~fletcher/Circle/list-faq.html |

This archive was generated by hypermail 2b30 : 12/08/00 PST