C++ class外的 >> 重载,输入流,重载示例。不应该定义类内的>>重载
2024-09-06 09:18:40
#include <iostream> // overloading "operator >> " outside class
// >> 应该定义在类之外。 ////////////////////////////////////////////////////////// class Rectangle
{
public:
Rectangle(int w, int h)
: width(w), height(h)
{}; ~Rectangle() {}; public:
int width;
int height;
}; ////////////////////////////////////////////////////////// std::istream&
operator>> (std::istream& is, Rectangle& rec)
{
is >> rec.width >> rec.height;
return is; } std::ostream&
operator<< (std::ostream& os, const Rectangle& rec) // 这里的 const 是必要的,rec应该是一个常量,请注意
{
os << rec.width << ", " << rec.height;
return os; } ////////////////////////////////////////////////////////// int main()
{
Rectangle a(40, 10);
std::cin >> a; std::cout
<< "a = " << a << std::endl; return 0;
}
最新文章
- Some About Spring
- java-内省与javabean
- Ubuntu Gnome 14.04.2 lts 折腾笔记
- Nikola的5项依赖注入法则
- [kipmi0]进程导致系统负载高
- postgresql downgrade issue
- scala实现Netty通信
- sed用法小结
- UML学习笔记之类之间的关系
- ImageMagick图片服务器
- C# 多线程学习笔记 - 1
- Windchill基本业务对象-文档
- python的内置函数time
- LeetCode 1:1. 两数之和
- nodejs的__dirname,__filename,process.cwd()区别
- Windows 10无法使用debug的解决方案
- 【ocp-12c】最新Oracle OCP-071考试题库(43题)
- 虚拟机下的zookeeper集群安装
- Android中自定义弹出PopupWindow后返回按键失效的问题解决
- Android 查看和管理sqlite数据库