这么写ostream& operator << (ostream& os, Point& pt)而不写成ostream operator << (ostream& os, Point& pt)
2016-03-18 13:42
253 查看
为什么这么写
ostream& operator << (ostream& os, Point& pt)
而不写成
ostream operator << (ostream& os, Point& pt)
ostream&这个返回值类型用定义成别名的形式吗??
在网上找到了答案如下:
如果写成这样
ostream operator << (ostream& os, Point& pt)
则:
Point a, b;
cout<<a<<b;
错误,只能写为:
cout<<a;
cout<<b;
原因在于
cout<<a<<b;
相当于:
(cout<<a)<<b;
第一个()中返回cout的临时变量,它不可以作为左值(因为operator << (ostream& os, Point& pt)的第一个参数是ostream&,而不是ostream),因而错误。
如果写成:
ostream& operator << (ostream& os, Point& pt)
则:
cout<<a<<b;
正确,因为它等同于
(cout<<a)<<b;
(cout<<a)返回cout的引用,即就是它自己,它可以再次作为左值,因而能够连着写这个输出流 。
ostream& operator << (ostream& os, Point& pt)
而不写成
ostream operator << (ostream& os, Point& pt)
ostream&这个返回值类型用定义成别名的形式吗??
在网上找到了答案如下:
如果写成这样
ostream operator << (ostream& os, Point& pt)
则:
Point a, b;
cout<<a<<b;
错误,只能写为:
cout<<a;
cout<<b;
原因在于
cout<<a<<b;
相当于:
(cout<<a)<<b;
第一个()中返回cout的临时变量,它不可以作为左值(因为operator << (ostream& os, Point& pt)的第一个参数是ostream&,而不是ostream),因而错误。
如果写成:
ostream& operator << (ostream& os, Point& pt)
则:
cout<<a<<b;
正确,因为它等同于
(cout<<a)<<b;
(cout<<a)返回cout的引用,即就是它自己,它可以再次作为左值,因而能够连着写这个输出流 。
相关文章推荐
- c++ io manipulator
- C++11:可变参数模板实现print输出参数
- c++文件读写
- linux下可变参数及宏定义封装函数
- ManageEngine-IT运维
- nginx 域名跳转一例~~~(rewrite、proxy)
- Golang Linux下面调用nohup启动程序无法返回问题,解决方法.
- docker 指定容器名字
- 【linux】rm[1]学习
- docker 指定容器名字
- 监控 Linux 性能的命令与工具
- [IMX6Q][Android5.1]移植笔记 --- Recovery mode的shell功能实现(sh+toolbox)
- Linux常用命令
- linux下常用命令
- opencv中的IplImage
- centos7.2之静态IP设置
- AODH: ALARM EVENTS IN OPENSTACK
- Linux常用压缩和解压命令
- svn 报错 Previous operation has not finished; run 'cleanup' if it was interrupted
- CentOS之许可