其中 ostream& operator << (ostream& stream, Test val2) 和 ostream operator << (ostream& stream, Test val2) 的区别如下:
如果写为ostream operator << (ostream& stream, Test val2)则:
Test a, b;
cout << a << b;
错误,只能写为:
cout<<a;
cout<<b;
原因在于:
cout<<a<<b;
相当于:
(cout<<a)<<b;
第一个()中返回cout的临时变量,它不可以作为左值。因而错误。
如果写为ostream& operator <<(ostream& stream, Test val2)则:
cout << a << b;
正确,因为它等同于
(cout<<a)<<b;
(acout<<a)返回cout的引用,即就是它自己,它可以再次作为左值。因而能够连着写这个输出流。
另外还有一些问题,留待以后解决。