previous | start | next

Return Type of operator<<

The type of cout is ostream

But functions ordinarily return by value!

This means that a copy is made of the value being returned.

This is a good thing if a local variable is being returned. Why?

But generally, copies should not be made of cout and cout will not be a local variable. It will be passed by reference as the first parameter to operator<<

So in order to not make a copy at the return, simply return the same cout by reference! So the declaration of operator<< should be

 ostream& operator<<(ostream& os, const Fraction& f);
     


previous | start | next