这么写ostream& operator << (ostream& os, Point& pt)而不写成ostream operator << (ostream& os, Point& pt)
来源:互联网 发布:acm算法视频百度云 编辑:程序博客网 时间:2024/06/06 01:33
为什么这么写
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的引用,即就是它自己,它可以再次作为左值,因而能够连着写这个输出流 。
1 0
- 这么写ostream& operator << (ostream& os, Point& pt)而不写成ostream operator << (ostream& os, Point& pt)
- ostream& operator << (ostream& os, Point& pt)
- ostream & operator<<()
- ostream&
- OSTREAM
- friend ostream& operator < <
- friend ostream& operator <<(ostream& out,const Money& x)
- friend ostream &operator<<(ostream &stream, const Date& dt);
- ostream & operator 重载操作流的注意事项
- 重载operator<<操作符—friend ostream& operator<<(ostream&,const Date&);
- C++:‘std::ostream& String::operator<<(std::ostream&, const String&)’ must take exactly one argument
- C++学习笔记:friend ostream &operator<<(ostream &stream, const Date& dt);
- istream/ostream
- ostream iterator
- 对friend std::ostream & operator<< 的简单学习
- ifstream ostream istream ostream 不能声明
- C++语言中ostream
- ostream这是啥玩意儿
- linux下可变参数及宏定义封装函数
- const关键字问题
- 单例模式
- Android二级分类列表ListView GirdView in ViewPager
- ”腾讯音视频服务”初次使用感想
- 这么写ostream& operator << (ostream& os, Point& pt)而不写成ostream operator << (ostream& os, Point& pt)
- ( mac os 10.10.1 ) letsencrypt +nginx 给自己的网站加把锁
- ImageView实现APP底部按钮切换页面
- 程序安装不成功 提示COM类工厂中CLSID为{6BF52-394A-11D3-00co4F79FAA6}的组件 可能的解决方法
- ZooKeeper 典型应用场景一览
- tomcat 启动错误 Error listenerStart org.apache.catalina.LifecycleException: Failed to start component
- 简明python教程 --C++程序员的视角(一):数值类型、字符串、运算符和控制流
- MethodHandle(方法句柄)
- 如何实现Zbrush 4R7中SubTool的添加和删除