C++ class 内的 () 重载示例
2024-09-04 06:54:59
#include <iostream> // overloading "operator () " outside class ////////////////////////////////////////////////////////// class Rectangle
{
public:
Rectangle(const int w, const int h)
: width(w), height(h)
{}; ~Rectangle() {};
int& operator() (const size_t i); public:
int width;
int height;
}; ////////////////////////////////////////////////////////// int &
Rectangle::operator()(const size_t i)
{
if (i == 0)
return width;
else
return height;
} ////////////////////////////////////////////////////////// std::ostream&
operator<< (std::ostream& os, const Rectangle& rec)
{
os << rec.width << ", " << rec.height;
return os; } ////////////////////////////////////////////////////////// int main()
{
Rectangle a(40, 10); std::cout
<< "w = " << a(0) << std::endl // 输出 40
<< "h = " << a(1) << std::endl // 输出 10
; return 0;
}
最新文章
- 轻量级“集合”迭代器-Generator
- Ubuntu安装SSH服务器故障分析及解决办法(错误1:E:软件包 openssh-server 还没有可供安装的候选者,错误2:E: 无法修正错误,因为您要求某些软件包保持现状,就是它们破坏了软件包间的依赖关系)
- JavaScript(四) Window窗体操作
- AccessHelper 需修改
- ext grid 子表格
- 关于ORACLE隐式转换后性能问题
- lodash接触:string-capitalize
- iOS代码实践总结
- gitlab 安装报错:Could not find modernizr-2.6.2 in any of the sources
- lua 函数回调技巧
- Javaweb 第15天 web练习和分页技术
- tp5上传图片添加永久素材到微信公众号
- cocos2d-x工作小记
- jquery库的cookie用法
- CentOS7用yum快速搭建LAMP平台
- XSS理解与防御
- Sublime Keymap 设置
- jenkins安装及配置-centos6.9
- Sequel Pro for Mac(MySQL 数据库管理工具)破解版安装
- Python进阶【第三篇】Python中的基本数据类型
热门文章
- 在iframe 中视频可以正常播放,但是就是不能全屏。解决方法
- C# FlagAttriute 的 小妙招
- jQuery总结01_jq的基本概念+选择器
- iOS中UICollectionView添加头视图
- 使用docker部署filebeat和logstash
- 苹果 macOS 安装 Source Code Pro
- 反转字符串中的单词 III
- HTML连载53-网易注册界面实战之content的头部、content注册信息
- C语言程序设计100例之(19):欢乐的跳
- Spring Cloud Alibaba 实战(十三) - Sleuth调用链监控