I am learning about streaming. The standard streams provide the << operator which can be declared as:
ostream& operator<<(stream& os, CLASS& rc);
Why is it impossible to declare it as this?
ostream& operator>>(CLASS& rc, stream& os);
Then I may be able to do something like:
rc.something >> os;
as part of its implementation.
Edit as people have helped me learn more about this, I am thankful.
However I am stuck at how to implement actually it.
I have tried
ostream& operator >> (const SomeClass& refToCls, stream& os)
{
refToCls.iVar >> os;
return os;
}
but it fails. How can I fix it?
In fact it’s possible to define
But then you must chain it like this:
The
>>operator is left-associative, so by default this:is equivalent to:
which has a wrong meaning.