我知道我应该从“operator<<”返回“ostream&”,以便能够像这样“链接”操作员
cout<<a<<b<<c;
但是,在下面的代码中,我没有返回“ostream&”,并且仍然可以进行链接。为什么?
#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;
}
输出
[1,2]
[3,4]
[5,6]