Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

partial solution to cout << 'a' producing 97

0 views
Skip to first unread message

Mark Rafter

unread,
Oct 15, 1986, 12:15:48 PM10/15/86
to
If you add the following (Steve Rumsby's idea):

ostream& put(char); // put('a') writes a
ostream& operator<<(const char &c){ return this->put(c);}

to the class ostream BEFORE the declaration of the operator

ostream& operator<<(int a) { return *this<<long(a); }

and delete the original declaration of put(char) , then the program:

#include <stream.h>
main()
{
char b = 'b';
cout << char('a') << b;
}

will output ``ab'' rather than ``9798''. However there are the following
(related) quirks:

1 the order of the declarations is important
2 the new << operator must be a member of ostream

I cannot find any justification for these two points in the C++ book -
is there any?
----------------------------------
Mark Rafter, Computer Science, Warwick Univ, Coventry CV4 7AL, ENGLAND
..!ukc!warwick!rafter +44 203 523364

0 new messages