when overloadind input/output stream operator, why return type??

J

jaivrat

When we overload the >> operator then why do we need to return the
istream by reference ? Why can't we do a void return?
(Quoting from a book )
e.g

istream & operator >>(istream & din, VectClass &v){
for( int i=0; i<= 3; i++)
din >> v;
return din;
}
 
M

Mike Wahler

jaivrat said:
When we overload the >> operator then why do we need to return the
istream by reference ?

You don't *have* to, but if you don't, you won't be
able to 'chain' calls such as:

cin >> a >> b >> c;
Why can't we do a void return?

You can if you like, but the function's use
will be more limited. Instead of the above,
you'd have to write:

cin >> a;
cin >> b;
cin >> c;

The choice is yours. :)
(Quoting from a book )
e.g

istream & operator >>(istream & din, VectClass &v){
for( int i=0; i<= 3; i++)
din >> v;
return din;
}


Which book are you reading that does not explain this?

-Mike
 
M

Mike Wahler

puzzlecracker said:
comp.lang.c++

That's not a book.

IMO one cannot learn a programming language by only
reading Usenet articles. You won't get far without
books.

-Mike
 

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
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top