By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,304 Members | 3,172 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,304 IT Pros & Developers. It's quick & easy.

<< for outputting chars need not be member

P: n/a
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

Jan 12 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On 2007-01-12 22:39, Diwa wrote:
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).

--
Erik Wikström
Jan 12 '07 #2

P: n/a

Erik Wikström wrote:
On 2007-01-12 22:39, Diwa wrote:
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 ?

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.
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).
I am still not clear why could we not have "operator << (char c)" as
a member of "basic_ostream" ?

Thanks
Diwakar

Jan 17 '07 #3

P: n/a
JE

<snip>
Diwa wrote:
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 ?
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

Jan 17 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.