Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > << for outputting chars need not be member

Reply
Thread Tools

<< for outputting chars need not be member

 
 
Diwa
Guest
Posts: n/a
 
      01-12-2007
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

 
Reply With Quote
 
 
 
 
=?ISO-8859-1?Q?Erik_Wikstr=F6m?=
Guest
Posts: n/a
 
      01-12-2007
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
 
Reply With Quote
 
 
 
 
Diwa
Guest
Posts: n/a
 
      01-17-2007

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

 
Reply With Quote
 
JE
Guest
Posts: n/a
 
      01-17-2007

<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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to truncate char string fromt beginning and replace chars instring by other chars in C or C++? Hongyu C++ 9 08-08-2008 12:18 PM
Stored Proc not Outputting David Lozzi ASP .Net 3 12-08-2005 01:11 AM
Floats to chars and chars to floats Kosio C Programming 44 09-23-2005 09:49 AM
receiving ??? chars instead of "special" chars M.Posseth ASP .Net Web Services 3 11-16-2004 07:00 PM
How would I use qsort to sort a struct with a char* member and a long member - I want to sort in order of the long member Angus Comber C Programming 7 02-05-2004 06:41 PM



Advertisments