运算符重载
来源:互联网 发布:js数组常用方法 编辑:程序博客网 时间:2024/05/17 08:30
一、操作符重载
用复数来实例:
#include <iostream>using namespace std;class Complex {public: Complex (int r = 0, int i = 0) :m_r (r), m_i (i) {} Complex const operator+ (Complex const& rhs) const { Complex sum (m_r + rhs.m_r, m_i + rhs.m_i); return sum; } void print (void) const { cout << m_r << '+' << m_i << 'i' << endl; }private: int m_r; // 实部 int m_i; // 虚部 friend Complex const operator- (Complex const& lhs, Complex const& rhs) { return Complex (lhs.m_r - rhs.m_r, lhs.m_i - rhs.m_i); }};
1.双目操作符
+/-
1)成员
Complex const operator+ (Complex const& rhs) const { … }
2)全局
Complex const operator+ (Complex const& lhs, Complex const& rhs) { … }
=/+=/-=
1)成员
Complex& operator+= (Complex const& rhs) {
…
return *this;
}
2)全局
Complex& operator-= (Complex& lhs, Complex const& rhs) {
…
return lhs;
}
2.单目操作符
-/~
1)成员
Complex const operator- (void) { … }
2)全局
Complex const operator~ (Complex const& c) { … }
前++/前–
1)成员
Complex& operator++ (void) {
…
return *this;
}
2)全局
Complex& operator– (Complex& c) {
…
return c;
}
后++/后–
1)成员
Complex const operator++ (int) { … }
2)全局
Complex const operator– (Complex& c, int) { … }“
0 0
- 运算符重载 [++,--,+,<<; &&,||(不可重载)]
- 重载之运算符重载
- 重载赋值运算符
- C++运算符重载
- 运算符重载
- 运算符重载总结
- 运算符重载例子
- 运算符重载
- 运算符重载
- 基础:运算符重载
- 运算符重载
- 运算符重载
- 重载赋值运算符
- C#运算符重载
- C++-运算符重载
- 重载赋值运算符
- 运算符重载
- 有关运算符重载
- 啊
- 欢迎使用CSDN-markdown编辑器
- 非常实用的PHP常用函数汇总
- HDU - 3068 最长回文(manacher)
- 【线性代数公开课MIT Linear Algebra】 第十一课 矩阵空间和秩1矩阵
- 运算符重载
- house-robber
- webstorm license key
- 一张图读懂spark streaming
- 深入理解Java中的堆和栈
- Applet学习心得
- Objective-C的meta-class 详解
- 编写一个程序,一行行地读取输入行,直至到达文件尾。算出每行输入行的长度,然后把最长的那行打印出来。为了简单起见,你可以假定所有的输入行均不超过1000个字符
- SpringMVC框架下的文件上传(多文件)