Return value of operator<< function
c++
Solution
Falling off the end of a non-void function is undefined behavior. One possible undefined behavior is working as you'd expect it to were the return in place.
g++ offers a handy warning to prevent this from ever happening to you.
Problem
I understand I should return "ostream&" from "operator<<" to be able to "chain" the operator like this ``` cout<<a<<b<<c; ``` In the following code, however, I do not return the "ostream&" and the chaining is still possible. Why? ``` #include <iostream> using namespace std; class CComplexNumber{ float m_realPart; float m_imagPart; public: CComplexNumber(float r,float i):m_realPart(r),m_imagPart(i){} friend ostream& operator<<(ostream& lhs,CComplexNumber rhs){ lhs<<"["<<rhs.m_realPart<<","<<rhs.m_imagPart<<"]"<<endl; //MISSING RETURN STATEMENT! } }; int main() { CComplexNumber a(1,2); CComplexNumber b(3,4); CComplexNumber c(5,6); cout<<a<<b<<c; return 0; } ``` OUTPUT ``` [1,2] [3,4] [5,6] ```