<< for outputting chars need not be member

D

Diwa

In TC++PL 3edn, in section 21.2.2, Stroustrup
notes that put( ) and write( ) simply write chars.

Therefore, << for outputting chars need not be a member

What does he mean here ?

Also, class basic_ostream does not have
operator << (char ch)

Thanks,
diwakar
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

In TC++PL 3edn, in section 21.2.2, Stroustrup
notes that put( ) and write( ) simply write chars.

Therefore, << for outputting chars need not be a member

What does he mean here ?

Imagine for a second that the << operator was a member of basic_ostream,
that would mean that typing
std::cout << "Hello";
would actually be a call to
std::cout.operator<<("Hello");

What would then happen when you make your own class myClass that you
want to be able to print? That would mean that you would have to change
basic_ostream to add an overloaded version of the << operator for
myClass. But since you didn't write the basic_ostream and might not have
the code you would not be able to.

By not making the << operator a member of basic_ostream you solve this
problem since anyone then can create their own overloaded function which
prints their class.

So what actually happens is that your 'std::cout << "Hello";' calls a
function something like this (I skip some of the template-stuff)
basic_ostream& operator<<(basic_ostream& o, const char* c);
so the call is actually
basic_ostream& operator<<(std::cout, "Hello");

So all you have to do to make myClass easily printable is implement a
function like
basic_ostream& operator<<(basic_ostream& o, myClass& m);
Also, class basic_ostream does not have
operator << (char ch)

Since it's not a member, but if you look at the list of functions right
at the end of 21.2.2 you'll see the non-member function (the second one
in my book).
 
D

Diwa

Erik said:
By not making the << operator a member of basic_ostream you solve this
problem since anyone then can create their own overloaded function which
prints their class.

But basic_ostream does have many member << operators
for built in types except char.
Since it's not a member, but if you look at the list of functions right
at the end of 21.2.2 you'll see the non-member function (the second one
in my book).

I am still not clear why could we not have "operator << (char c)" as
a member of "basic_ostream" ?

Thanks
Diwakar
 
J

JE

I am still not clear why could we not have "operator << (char c)" as
a member of "basic_ostream" ?

I think it's more like operator<<(char) doesn't _have_ to be a member,
because what is written doesn't depend on format. For example, if
you're streaming a bool, you might get '1' or 'true' written, or for a
double, what is written depends on, among other things, precision.
Streaming char c is just writes char c.
- -
JE
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top