Hi forum
Take this example code:
What would the last statement be equivalent to?Code:class Object { public: Object(const char *String); Object &operator<<(const Object &LHS); } Object A, B, C, D; A << B << "test" << C;
A)
ORCode:A.operator<<(B); Object Temp("test"); A.operator<<(Temp); A.operator<<(C);
B)
I believe it is A, but why then do so many tutorials on operator<<-overloading say something like:Code:Object Temp("test"); A.operator<<( B.operator<<( Temp.Operator<<( C ) ) );
"We have to return the [in this case class Object] object, since this allows us to chain multiple objects to be output on a single line (e.g.: A << "blah" << B << C; )."
If the correct equivalent is A, this is wrong, because you don't NEED to return the object for that. You would only have to return it for things like:
Am I right or am I mistaken?Code:if( (A << B << C) == D) return;
Any help is highly appreciated![]()





Reply With Quote