C++编程思想学习笔记---第12章 运算符重载
来源:互联网 发布:oa软件下载 编辑:程序博客网 时间:2024/06/02 02:09
运算符重载只是一种语法上的方便,只是另一种形式的函数调用。
应当说明,运算符重载不应该被滥用,只有在能使涉及类的代码更易写,特别是更易读时,才有理由重载运算符
12.1 运算符重载的语法:
#include <iostream>using namespace std;class Integer{ int i;public: Integer(int ii): i(ii) {} const Integer operator+(const Integer& rv) const { cout << "operator+" << endl; return Integer(i + rv.i); } Integer& operator+=(const Integer& rv) { cout << "operator+=" << endl; i += rv.i; return *this; } int GetValue() const { return i; }};int main(){ cout << "built-in types:" << endl; int i = 1; int j = 2; int k = 3; k += i + j; cout << "k = " << k << endl; cout << "user-defined types:" << endl; Integer ii(1), jj(2), kk(3); kk = ii + jj; cout << "Int.kk = " << kk.GetValue() << endl; kk += ii; cout << "Int.kk = " << kk.GetValue() << endl; return 0;}
对于二元运算符,唯一的参数是出现运算符右侧的那个操作数,运算符左边的操作数是调用该重载后的运算符的对象。
12.2 可重载的运算符
几乎所有c中的运算符都可以重载,但其行为时很受限制的。特别是不能使用c中没有意义的运算符(例如用**代表求幂),不能改变运算符的优先级,不能改变运算符的参数个数
12.2.1 一元运算符
#include <iostream>using namespace std;class Integer{ long i; Integer* This() { return this; }public: Integer(long ll = 0): i(ll) {} friend const Integer& operator+(const Integer& a); friend const Integer operator-(const Integer& a); friend const Integer operator~(const Integer& a); friend Integer* operator&(Integer& a); friend int operator!(const Integer& a); friend const Integer& operator++(Integer& a); friend const Integer operator++(Integer& a, int); friend const Integer& operator--(Integer& a); friend const Integer operator--(Integer& a, int);};const Integer& operator+(const Integer& a){ cout << "+Integer\n"; return a;}const Integer operator-(const Integer& a){ cout << "-Integer\n"; return Integer(-a.i);}const Integer operator~(const Integer& a){ cout << "~Integer\n"; return Integer(~a.i);}Integer* operator&(Integer& a){ cout << "&Integer\n"; return a.This();// This() is a private function, can be called? dubious...}int operator!(const Integer& a){ cout << "!Integer\n"; return !a.i;}const Integer& operator++(Integer& a){ cout << "++Integer\n"; a.i++; return a;}const Integer operator++(Integer& a, int){ cout << "Integer++\n"; Integer before(a.i); a.i++; return before;}const Integer& operator--(Integer& a){ cout << "--Integer\n"; a.i--; return a;}const Integer operator--(Integer& a, int){ cout << "Integer--\n"; Integer before(a.i); a.i--; return before;}void f(Integer a){ +a; -a; ~a; Integer *ip = &a; !a; ++a; a++; --a; a--;}int main(void){ Integer a; f(a); return 0;}
打印的内容为:
+Integer
-Integer
~Integer
&Integer
!Integer
++Integer
Integer++
–Integer
Integer–
这里容易产生混淆的运算符是自增和自减运算符。实际操作的结果是:
++a : 调用operator++(a)
a++ : 调用operaotr++(a, int)
自减运算符也是如此。
12.2.2 二元运算符
(代码太长,请参阅原书)
operator=只允许作为成员函数,而且在写=时应该小心类型的转换等。
12.2.3 参数和返回值
(待续)
0 0
- C++编程思想学习笔记---第12章 运算符重载
- C++编程思想----第12章、运算符重载
- C++编程思想 12章运算符重载
- C++编程思想笔记——运算符重载
- C++编程思想学习——运算符重载
- C++编程思想读书笔记之 第十一章 运算符重载
- C++编程思想--运算符重载
- C++编程思想之运算符重载
- 《Python学习手册》学习笔记(29)之第29章运算符重载(关键词:编程语言/Python/OOP/面向对象/重载)
- [学习笔记][Java编程思想]第3章:操作符
- 【笔记-C++】 运算符重载
- 《C++编程思想》拷贝构造函数,运算符重载,虚函数 笔记
- 运算符重载 学习笔记
- 运算符重载学习笔记
- 【C++】【学习笔记】【025-027】运算符重载
- 【C++】学习笔记五十一——运算符重载
- C++学习笔记——第11章 运算符法重载
- JAVA 编程思想 第4版 学习笔记 - 第五章
- 注册表reg文件编写
- JS设计模式-单例模式
- SVN
- 【容斥原理-求区间内与n互质的数】HDOJ Co-prime 4135
- hdu3045 Picnic Cows
- C++编程思想学习笔记---第12章 运算符重载
- 初学python
- ADS2009U1安装破解步骤及文件下载地址(亲测XP_WIN7成功)
- iOS图片的多选择,删除,和本地显示
- zend studio自动添加文件注释和方法注释
- 进入实战,开发酷欧天气(三)
- Service的生命周期与Activity生命周期区别
- 反射入门(转)
- 白话经典算法系列之二 直接插入排序的三种实现