🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: net.lang.c++
Article #342

Overloading operators on classes' ops

#342
From: Joseph S. D. Yao
Date: Fri, 10 Oct 1986 15:55
42 lines
2255 bytes
I tried to do something that I thought would work in C++.  I think
I understand why it didn't work in C++ the implementation, but am
wondering whether I had it right in C++ the language.

Theoretically, one should be able to overload an operator with a
different routine for different type arguments.  Char and int should
be different types, despite automatic promotion of constants and
function and operator arguments, right?

[Please forgive format-for-brevity in the following.]

#include <stream.h>
main(){ char ch = 'z'; cout << ch << "\n"; }

prints 122, of course, as the manual and comments say it should.
Adding AFTER this:

ostream &operator<<(ostream &s, char c) {
  static char str[2] = "\0"; *str = c; return(s << str); }

gives NO CHANGE in function, and NO error message.  Well, of course,
the C++ compiler hasn't seen the new overloaded operator yet.  BUT
when I move the above function before main(), I get:

  << defined both as operator <<() and ostream::operartor <<()

which is supposed to be legal!  (unless the args' types are the same.)

I am assuming that the implementation is treating chars as ints; but
I think I should be able to do this in the language.  Comments?

By the way: ostream's operator << is defined for:
  const char *, int, long, double, const streambuf &,
  const whitespace &, const common &;
but note that istream's >> operator is defined for:
  char *, char &, short &, int &, long &, float &, double &,
  streambuf &, whitespece &, common &;
so it appears here that char is not being confused with int!
-- 

	Joe Yao		hadron!jsdy@seismo.{CSS.GOV,ARPA,UUCP}
			jsdy@hadron.COM (not yet domainised)
Technical Details
Message-ID: <594@hadron.UUCP>
Path: arc1-ca.pugleaf.net!utzoo!mnetor!seismo!sundc!netxcom!hadron!jsdy

Show Full Headers
Complete Article Headers
Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP
Path: arc1-ca.pugleaf.net!utzoo!mnetor!seismo!sundc!netxcom!hadron!jsdy
From: jsdy@hadron.UUCP (Joseph S. D. Yao)
Newsgroups: net.lang.c++
Subject: Overloading operators on classes' ops
Message-ID: <594@hadron.UUCP>
Date: Fri, 10-Oct-86 11:55:12 EDT
Article-I.D.: hadron.594
Posted: Fri Oct 10 11:55:12 1986
Date-Received: Sat, 11-Oct-86 20:02:44 EDT
Reply-To: jsdy@hadron.UUCP (Joseph S. D. Yao)
Organization: Hadron, Inc., Fairfax, VA
Lines: 42
Keywords: char vs. int, overload, operators, <<
Summary: My confusion