Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why we need to return reference to istream/ostream while overloading >> and << operators?

What happens if I do not return din or dout, actually I'm reading a book in which writer returns back stream references

istream & operator>>(istream &din,vector &a)
{
    for(int i=0;i<size;i++)
    din>>a.v[i];
    return din;
}

ostream & operator<<(ostream &dout,vector &a)
{
    dout<<"("<<a.v[0];
    for(int i=1;i<size;i++)
    dout<<", "<<a.v[i];
    dout<<")";
    return dout;
}
like image 574
Rajat Avatar asked Mar 07 '15 08:03

Rajat


People also ask

What is the return type of overloading cascading insertion operator?

The overloaded stream insertion operator function then generates a function call to print r1 and (if written correctly) returns a reference to the ostream object that was passed into the function ( cout , in this case).

What is Ostream operator?

A function that takes and returns a stream object. It generally is a manipulator function. The standard manipulators which have an effect when used on standard ostream objects are: manipulator.

Can we overload the insertion and extraction operator?

The stream insertion and stream extraction operators also can be overloaded to perform input and output for user-defined types like an object. Here, it is important to make operator overloading function a friend of the class because it would be called without creating an object.


2 Answers

The reason is a combination of several facts.

  1. You want to be able to chain input and output operations as in

    in  >> x >> y;
    
    out << z << std::precision(10) << t << std::endl;
    

    so you must return something that allows operator<< again.

  2. Since you want your operator to work on any istream, i.e. any object derived from std::istream, you cannot define

    operator<<(istream_type, object);    // take istream by value
    

    since this would only work for the specific istream type istream_type, but not for a generic istream. For that one must use polymorphism, i.e. either take a reference or a pointer (which will be a reference or pointer to a class derived from std::istream).

  3. Since you only have a reference to the istream, you cannot return the istream object itself (which may be of a type not even defined at the point of the definition of operator<<) but only the reference you've got.

    One could get around this restriction by defining operator<< a template and take and return the istream_type by value, but that requires the istream type to have a copy constructor, which it may well not have for good reasons.

  4. In order to envoke polymorphism one could, in principle, use pointers (to streams) rather than references. However, operator<<(stream*,const char*) is not allowed in C++ (at least one operand must be of class or enumeration type).

    Thus, with stream pointers one must use function-call syntax and you're back with C-style fprintf(stream*, args...).

    Moreover, pointers can be null or dangling, which in fact is their default state (when declared without initializer), while a reference can be assumed to be valid (it cannot be declared without initializer).

like image 87
Walter Avatar answered Oct 17 '22 06:10

Walter


In this case when the reference is returned you can combain the operator in a chain. For example

std::cout << "Hello " << "Rajat Verma";

This is equivalent to the following calls of the operator

operator <<( operator <<( std::cout, "Hello" ), "Rajat Verma" );
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
              returns reference to std::cout 
like image 4
Vlad from Moscow Avatar answered Oct 17 '22 07:10

Vlad from Moscow