Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why overloading of operator<< must return by reference?

I want to print out an object of a user-defined type, like this cout << ob1; so I want to overload operator<< and I want to return by value not by reference but it gives me an error: in two files named : iosfwd and ios_base.h

ostream operator<<( ostream& out, cat& rhs){
    out << rhs.a << ", " << rhs.b << endl;
    return out ; 
}

1)Is it because it can't create a new ostream object, this is why it have to return by reference ?

but when I return by reference like this :

ostream& operator<<( ostream& out, cat& rhs){
    out << rhs.a << ", " << rhs.b << endl;
    return out ;
}

it works fine.
2)any explanation ?

like image 417
AlexDan Avatar asked Mar 10 '12 13:03

AlexDan


People also ask

Why do we return by reference in operator overloading?

On the other hand, the usual semantics of operator+= involves returning the left hand side object, hence returning by reference is usually correct to avoid an unnecessary copy should there be operator chaining.

Why arguments are passed as reference variable in case of << and >> operator overloading?

Advantages of passing by reference: It allows us to have the function change the value of the argument, which is sometimes useful. Because a copy of the argument is not made, it is fast, even when used with large structs or classes. We can pass by const reference to avoid unintentional changes.

Can we overload << operator?

We can overload the '>>' and '<<' operators to take input in a linked list and print the element in the linked list in C++. It has the ability to provide the operators with a special meaning for a data type, this ability is known as Operator Overloading.

What is the advantage of overloading the << and >> operators?

By overloading the operators, we can give additional meaning to the operators like +-*/=.,= etc., which by default are supposed to work only on standard data types like int, float, char, void etc.


2 Answers

In the first example, you return a copy of the stream object which is not allowed because the copy-constructors (and copy-assignment as well) of all the stream classes in C++ has been disabled by having them made private.

Since you cannot make a copy of a stream object, you're required to return it by reference, which you're doing in the second example which is why it is working fine.

You may choose to return nothing at all (i.e you can make the return type void), but if you do so, then you would not be able to chain as stream << a << b. You've to write them separately as stream <<a and then stream << b.

If you want to know why copying of stream objects is disabled, see my answer here:

  • Why copying stringstream is not allowed?
like image 165
Nawaz Avatar answered Oct 20 '22 18:10

Nawaz


Streams are non copyable because copying a stream doesn't really make sense, a stream is unique (you can't jump in the same river twice kind of thing). return by value is in C++03 at least copying transaction.

If there are reason you want to return by value, return by reference is the correct version.

like image 26
111111 Avatar answered Oct 20 '22 18:10

111111