Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   << for outputting chars need not be member (http://www.velocityreviews.com/forums/t459797-for-outputting-chars-need-not-be-member.html)

Diwa 01-12-2007 09:39 PM

<< for outputting chars need not be member
 
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?= 01-12-2007 10:44 PM

Re: << for outputting chars need not be member
 
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

Diwa 01-17-2007 03:47 PM

Re: << for outputting chars need not be member
 

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


JE 01-17-2007 05:51 PM

Re: << for outputting chars need not be member
 

<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



All times are GMT. The time now is 08:43 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.