重载操作符 operator overloading 学习笔记
重载操作符,只是另外一种调用函数的方法和表现方式,在某些情况它可以让代码更简单易读。注意不要过度使用重载操作符,除非它让你的类更简单,让你的代码更易读。
1语法
如下:
其中友元,关键字不是必须的,但是当你需要读参数类的内部变量时候,声明就需要加上friend.
friend Money operator +(const Money & amount1,const Money& amount2);
friend bool operator ==(const Money & amount1,const Money& amount2); friend Money operator -(const Money & amount1,const Money& amount2);
friend Money operator -(const Money & amount1);
//
参数写成上面的形式,在上篇博客中解释到,是为了提高效率,同时保证不改变该类的参数。
2重载操作符的一些规则:
a.重载一个操作符时,至少一个参数属于类的类型
b.重载的操作符,可以是一个类的友元,也可以是该类的成员,也可以是一个普通(非友元)函数。
c.不能新建一个操作符,只能对现有操作符进行重载,如:+,-,*,/,%等
d.不能改变操作符接受的参数数目。如上文,都是对 ‘-’ 进行重载,当有两个参数时候,进行的是a-b的重载,有一个参数时候,进行的是-a的重载。
3重载>> and <<
语法和上面类似。注意,返回的是一个流,对于一个流,就不能简单的返回留的值,它的值可能是一个文件,或一个屏幕等,所以你要返回流本身,而不是流的值。所以必须在返回类型的名称末尾添加&,即“引用”。这表示返回的是对象本身,而不是对象的值。
friend istream& operator >>(istream& ins,Money& amount);
friend ostream& operator <<(ostream& out,Money& amount);
4示例代码
//下面是一个小实例,表示moeny类,并重载了一些操作符,供参考
// Money.cpp : 定义控制台应用程序的入口点。
// #include "stdafx.h"
#include<iostream>
#include<cstdlib>
#include<cctype> using namespace std; class Money{
public: friend Money operator +(const Money & amount1,const Money& amount2);
friend bool operator ==(const Money & amount1,const Money& amount2); friend Money operator -(const Money & amount1,const Money& amount2);
friend Money operator -(const Money & amount1); friend istream& operator >>(istream& ins,Money& amount);
friend ostream& operator <<(ostream& out,Money& amount); Money(){all_cents=;} Money(long dollars,int cents){
all_cents=dollars*+cents;
} Money(long dollars):all_cents(dollars*){} double get_value() const{ return all_cents;} void input(istream& ins);
void output(ostream& outs) const; private:
long all_cents;
};
Money operator +(const Money & amount1,const Money& amount2){ return Money(,amount1.all_cents+amount2.all_cents);
}
bool operator ==(const Money & amount1,const Money& amount2){ return amount1.all_cents == amount2.all_cents;
}
void Money::input(istream& ins){
cout<<"please enter dollars:";
long dol,cent;
ins>>dol;
cout<<endl<<"please enter cents:";
ins>>cent; all_cents=cent+dol*; }
istream& operator >>(istream& ins,Money& amount){ cout<<"please enter dollars:";
long dol,cent;
ins>>dol;
cout<<endl<<"please enter cents:";
ins>>cent; amount.all_cents=cent+dol*;
return ins;
} void Money::output(ostream& out) const{
out<<"all the money you have is:"<<all_cents<<" cents"<<endl;
} ostream& operator <<(ostream& out,Money& amount){ out<<"all the money you have is:"<<amount.all_cents<<" cents"<<endl;
return out; }
Money operator -(const Money & amount1,const Money & amount2){ return Money(,amount1.all_cents-amount2.all_cents); }
Money operator -(const Money & amount1){
return Money(,-amount1.all_cents); }
int _tmain(int argc, _TCHAR* argv[])
{ Money a(),b(,); Money c=a+b; Money d=c-b; cout<<a<<b<<c<<d<< (d==a);
return ;
}
5返回值优化 Return optimization
如果’+’重载,如果如下实现,那么要经历3个过程:
a. temp调用构造函数
b. temp把值赋给要返回的值,需要调用“复制构造函数”
c.函数结束时候,temp调用析构函数
Money operator +(const Money & amount1,const Money& amount2)
{
Money temp;
temp.all_cents=amount1.all_cents+amount2.all_cents;
return temp;
}
如程序改下成如下形式(上面示例代码的形式):
Money operator +(const Money & amount1,const Money& amount2){
return Money(,amount1.all_cents+amount2.all_cents);
}
这样,编译器会直接调用构造函数用于返回的对象,它甚至不需要调用析构函数,因为你没有真正构造一个本地的对象。这种直接返回值得方法,叫做“返回值优化(return value optimization)”.
本人水平有限,怀着分享学习的态度发表此文,欢迎大家批评,交流。感谢您的阅读。
欢迎转载本文,转载时请附上本文地址: http://www.cnblogs.com/Dzhouqi/p/3393257.html
另外:欢迎访问我的博客 http://www.cnblogs.com/Dzhouqi/
重载操作符 operator overloading 学习笔记的更多相关文章
- C++重载操作符operator
operator是C++关键字,用于对C++进行扩展: 1.可以被重载的操作符:new,new[],delete,delete[],+,-,*,/,%,^,&,|,~,!,=,<,> ...
- 重载操作符 'operator'
operator 是 C++ 的(运算符的)重载操作符.用作扩展运算符的功能. 它和运算符一起使用,表示一个运算符函数,理解时应将 [operator+运算符] 整体上视为一个函数名. 要注意的是: ...
- C++学习笔记-操作符重载
操作符重载(operator overloading)是一种形式的C++多态,C++将操作符重载扩展到用户自定义的类型,如允许使用+将两个自定义的对象相加,编译器将根据操作数的数目和类型决定使用那种加 ...
- c++学习笔记之函数重载和模板理解
1.函数重载: C++ 不允许变量重名,但是允许多个函数取相同的名字,只要参数表不同即可,这叫作函数的重载(其英文是 overload).重载就是装载多种东西的意思,即同一个事物能完成不同功能. 所谓 ...
- 《Think Python》第17章学习笔记
目录 <Think Python>第17章学习笔记 17.1 面向对象的特性(Object-oriented features) 17.2 打印对象(Printing objects) 1 ...
- boost uuid 学习笔记
#include <vector>#include <iostream>#include <boost/uuid/uuid.hpp>#include <boo ...
- Kotlin语言学习笔记(6)
运算符重载(Operator overloading) 一元运算符 Expression Translated to +a a.unaryPlus() -a a.unaryMinus() !a a.n ...
- [置顶] operator overloading(操作符重载,运算符重载)运算符重载,浅拷贝(logical copy) ,vs, 深拷贝(physical copy)
operator overloading(操作符重载,运算符重载) 所谓重载就是重新赋予新的意义,之前我们已经学过函数重载,函数重载的要求是函数名相同,函数的参数列表不同(个数或者参数类型).操作符重 ...
- C++基础学习笔记----第十三课(操作符重载-下)
本节主要讲使用成员函数重载操作符,包括[],=,(),->四种操作符的重载以及&&和||的问题. 类的成员函数进行操作符重载 基本概念 类的成员函数也可以进行操作符的重载.类的普 ...
随机推荐
- WCF 宿主与通信模式(二)
宿主 每个WCF服务都必须托管在Windows进程中,该进程称为宿主进程(host process) 单个宿主进程可以托管多个服务,相同的服务类型也可以托管在多个宿主进程中. wcf中托管服务一般有一 ...
- 【ASP.NET+MVC4+Web+编程】读书笔记
模型:数据和业务逻辑 视图:展示 控制器:接收视图输入数据,通过模型层业务逻辑处理后 返回给视图 分离关注点(模型 视图 控制器).惯例优先原则 browser-->routing-->c ...
- angular $q服务的用法
Promise是一种和callback有类似功能却更强大的异步处理模式,有多种实现模式方式,比如著名的Q还有JQuery的Deffered. 什么是Promise 以前了解过Ajax的都能体会到回调的 ...
- [大牛翻译系列]Hadoop系列性能部分完结
Hadoop系列性能部分完结.其它的部分发布时间待定. Hadoop系列将不再一日一篇,开始不定期发布.
- js判断选择时间不能小于当前时间的代码
判断选择时间不能小于当前时间的方法有很多,在本文为大家详细介绍下使用js是如何实现的,感兴趣的朋友可以尝试操作下 复制代码代码如下: var controldate; function checkD ...
- javascript dom追加内容的例子
javascript dom追加内容的使用还是比较广泛的,在本文将为大家介绍下具体的使用方法. 例子: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML ...
- 第一章 基本的SQL语句 (SQL基础)
1. 查询数据库系统时间,常以服务器默认的格式进行显示(根据数据库的字符集而定): 注意:dual 为数据库中的虚表,隶属于管理员 sys 用户,但所有的用户都可以访问:无实际意义,仅充当select ...
- GIS业务逻辑
三维怎么加载数据文件? OpenFileDialog frm = new OpenFileDialog(); frm.Filter = "文件数据集|*.tile|多时相数据集|*.Temp ...
- EXTJS 4.2 资料 控件之combo 联动
写两个数据源: 1.IM_ST_Module.js { success:true, data:[ { ModuleId: '1', ModuleName: '资讯' } , { ModuleId: ' ...
- 开发之前的思考-UI结构设计
UI结构设计遵循的一些要点 1.尽量不要让UI作为Camera的子物体 因为UI和摄像机敏感的关系,尽量不要将UI作为摄像机的子物体,避免出现一些因为透视(3D UI)等问题导致的视觉Bug. 2.尽 ...