I have an overloaded << operator from my reckful class implemented as follows:
ostream& operator << (ostream& os, const reckful& p)
{
os << p.PrintStuff();
return os;
}
PrintStuff() just being a member function of reckful that returns a string.
The way I understand things, if I were to write something like cout << reckobject << endl; in main(), cout << reckobject would take precedence and my overloaded << (using cout as the left operand and reckobject as the right operand) would return the ostream object os, leaving the expression os << endl; to be evaluated which would output the string and then end the line. So, the first << is the one I declared and the second is the standard << right?
However, my main question is… what is the sequence of events, which are the left and right operands, and which << operators are which when I run a statement like this:
cout << "reckful object = " << reckobject << "!" << endl;
Why does this work if there isn’t an ostream object and a reckful object on either side of one << ?
Thanks.
If you notice standard way to implement
<<it returns the ostream itself. This is the critical pieceSo something like
will be called once for
This function call will return a
ostreamwith which the second call will be madenamely
so on an so forth.
You can test is out by implementing your
<<asin which case you can do
but not
The operators make it harder to understand but consider this
PointclassThe way
setXandsetYare defined, allowsThis is the same mechanism
<<is using to chain function calls.