C++ 的 +,加号重载示例
#include <iostream> // overloading "operator + "
// 要考虑加法的形式
// a+1
// a+a
// 1+a ////////////////////////////////////////////////////////// class Rectangle
{
public:
Rectangle(const int w, const int h)
: width(w), height(h)
{}; ~Rectangle() {};
Rectangle operator+ (const int& i) const; // a+1,这里不能返回引用
Rectangle operator+ (const Rectangle& i) const; // a+a
// 1+a,定义在class之外 public:
int width;
int height;
}; ////////////////////////////////////////////////////////// Rectangle
Rectangle::operator+(const int& i) const // a+1
{
Rectangle r(*this);
r.width += i;
r.height += i; return r;
} Rectangle
Rectangle::operator+(const Rectangle& rec) const // a+a
{
Rectangle r(*this);
r.width += rec.width;
r.height += rec.height; return r;
} ////////////////////////////////////////////////////////// std::ostream&
operator<< (std::ostream& os, const Rectangle& rec)
{
os << rec.width << ", " << rec.height;
return os; } Rectangle
operator+(const int i, const Rectangle& rec) // 1+a
{
Rectangle r(rec);
r.width += i;
r.height += i; return r;
} ////////////////////////////////////////////////////////// int main()
{
Rectangle a(40, 10); std::cout << "a+1 = " << a + 1 << std::endl;
std::cout << "a+a = " << a + a << std::endl;
std::cout << "1+a = " << 1 + a << std::endl; // 这种形式,先计算1+a,然后a+a,最后a+1。
std::cout
<< "a+1 = " << a + 1 << std::endl
<< "a+a = " << a + a << std::endl
<< "1+a = " << 1 + a << std::endl
; return 0;
}
C++ 的 +,加号重载示例的更多相关文章
- 计算时间:一个C++运算符重载示例
Time类是一个用于计算时间的类,其原型如下:程序清单11.1 mytime0.h // mytime0.h -- Time class before operator overloading #if ...
- C++ class外的 >> 重载,输入流,重载示例。不应该定义类内的>>重载
#include <iostream> // overloading "operator >> " outside class // >> 应该 ...
- C++ class 内的 () 重载示例
#include <iostream> // overloading "operator () " outside class //////////////////// ...
- C++ class 内的 [] 重载示例。
#include <iostream> // overloading "operator [] " inside class ///////////////////// ...
- C++ class 外的 ++ 重载,左++,右++,重载示例。
#include <iostream> // overloading "operator ++ " outside class // ++ 是一元操作符 /////// ...
- C++ class内的 ++ 重载,左++,右++,重载示例。
#include <iostream> // overloading "operator ++ " inside class // ++ 是一元操作符 //////// ...
- C++ class外的 << 重载,输出流,重载示例。不应该定义类内的<<重载
#include <iostream> // overloading "operator << " outside class // << 应该 ...
- C++ class内的 < 和 > 重载,大于号,小于号,重载示例。
#include <iostream> // overloading "operator = " outside class // < 和 > 是二元操作符 ...
- C++ class内的=重载,拷贝赋值函数copy op=,重载示例。必须是class内
#include <iostream> // overloading "operator = " inside class // = 是一元操作符.不写,编译器会提供 ...
随机推荐
- 获取本机的IP地址
/// <summary> /// 获取本机IP地址 /// </summary> /// <returns>本机IP地址</returns> publ ...
- Nexus-在项目中使用Maven私服,Deploy到私服、上传第三方jar包、在项目中使用私服jar包
场景 Ubuntu Server 上使用Docker Compose 部署Nexus(图文教程): https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/ ...
- SpringBoot中使用Zuul
Zuul提供了服务网关的功能,可以实现负载均衡.反向代理.动态路由.请求转发等功能.Zuul大部分功能是通过过滤器实现的,除了标准的四种过滤器类型,还支持自定义过滤器. 使用@EnableZuulPr ...
- npm 使用过程中报错问题-及npm使用
原文地址:https://blog.csdn.net/u013022210/article/details/77740519 1.以下为报错具体详情:node 8.1.2 版本问题:其他空间安装成功但 ...
- Python里的*args and **kwargs
http://book.pythontips.com/en/latest/args_and_kwargs.html https://stackoverflow.com/questions/339483 ...
- "(error during evaluation)" computed
在vue-cli搭建的去哪网app项目中使用了 computed 计算属性 computed计算属性在chrome插件中的 vue devtools 插件中报错 应该显示出来 computed 属 ...
- Cocos2d-x3.0网络通信学习(一)
配置:win7+Cocos2d-x.3.0+VS2012 摘要:建立基本的http通信并得到返回信息. 一.添加项目与编译库 1.添加头文件 在需要用到Http网络相关类的文件中加入头文件 #incl ...
- fiddler 进行Android/IOS代理配置抓包
1.准备:Android+IOS设备 下载:fiddler抓包工具,不是最新版的链接: 链接:https://pan.baidu.com/s/1BaBfu2H4xgpsh1wmkfC8aQ ...
- day97_11_29
一.数据存储到mongodb 爬取的数据如果需要存储到mongodb中,要通过item,定义一个存储类.再yield一个类. 数据存储的时候需要进过pipelines,再到setting中配置. fr ...
- JS去重的几种常见方法
JS数组去重的几种常见方法 一.简单的去重方法 // 最简单数组去重法 /* * 新建一新数组,遍历传入数组,值不在新数组就push进该新数组中 * IE8以下不支持数组的indexOf方法 * */ ...