1 原生的++操作符
对于 i++ 和 ++i 有什么区别呢?区别如下:
我们反汇编一下看看二者的区别到底在哪?
编程实验:原生前置++与后置++的区别
#include <iostream>
using namespace std;
int main() {
int i = 0;
i++;
++i;
return 0;
}
反汇编如下,我们可以看到对于单纯的 i++ 和 ++ i 来说从汇编代码上看没有任何区别。
为什么单纯的 i++ 和 ++i 是一样的呢?
- 现代编译器产品对代码进行优化,使得最终的二进制程序更加高效
- 优化后的二进制程序丢失了 C/C++ 的原生语义
- 不可能从编译后的二进制程序还原 C/C++ 程序
2 重载++操作符
++ 操作符可以被重载
- 全局函数和成员函数均可进行重载
- 前置 ++ 操作符不需要额外的参数,后置 ++ 操作符需要一个 int 类型的占位参数
// 28-2.cpp
#include<iostream>
using namespace std;
class Test
{
public:
Test(int i) : mValue(i) {}
int value() { return mValue; }
Test& operator ++ ()
{
++mValue;
return *this;
}
Test operator ++ (int) // 后置++需要int 类型的占位参数
{
Test ret(mValue);
++mValue;
return ret;
}
private:
int mValue;
};
int main()
{
Test t1(0);
Test t2 = t1++;
cout << t1.value() << endl;
cout << t2.value() << endl;
return 0;
}
前置 ++ 操作不需要 int 类型的占位参数,后置 ++ 参数需要一个 int 类型的占位参数。前置 ++ 操作直接操作当前对象,返回当前对象,后置 ++ 操作先用当前对象初始化临时对象,完成对当前对象的操作后返回临时对象。
编译运行:
$ g++ 28-2.cpp -o 28-2
$ ./28-2
1
0
前置 ++ 操作没有临时对象,效率较高。后置 ++ 操作生成临时对象,需要栈空间存储临时变量,同时需要调用构造函数创建对象,调用析构函数销毁对象,效率较低。
2.1 前置++与后置++区别总结:
对于基础类型的变量
- 前置 ++ 的效率与后置 ++ 的效率基本相同
对于类类型的对象 - 前置 ++ 效率高于后置 ++,尽量使用前置++操作符。
2.2 复数类的进一步改善
在【C++深度解析】20、操作符重载中我们重载了运算操作符 +,-,*,/,比较操作符 ==,!=,赋值操作符 = 实现了复数类,这里我们重载 ++ 操作符,对复数类进一步完善。
// Complex.h
#ifndef _COMPLEX_H_
#define _COMPLEX_H_
class Complex
{
public:
Complex(double a = 0, double b = 0);
double getA();
double getB();
double getModulus();
Complex operator + (const Complex& c);
Complex operator - (const Complex& c);
Complex operator * (const Complex& c);
Complex operator / (const Complex& c);
bool operator == (const Complex& c);
bool operator != (const Complex& c);
Complex& operator = (const Complex& c);
Complex& operator ++ ();
Complex operator ++ (int);
private:
double a;
double b;
};
#endif
// Complex.cpp
#include"Complex.h"
#include<math.h>
Complex::Complex(double a, double b)
{
this->a = a;
this->b = b;
}
double Complex::getA()
{
return a;
}
double Complex::getB()
{
return b;
}
double Complex::getModulus()
{
return sqrt(a*a + b*b);
}
Complex Complex::operator + (const Complex& c)
{
double na = a + c.a;
double nb = b + c.b;
Complex ret(na, nb);
return ret;
}
Complex Complex::operator - (const Complex& c)
{
double na = a - c.a;
double nb = b - c.b;
Complex ret(na, nb);
return ret;
}
Complex Complex::operator * (const Complex& c)
{
double na = a * c.a - b * c.b;
double nb = a * c.b + b * c.a;
Complex ret(na, nb);
return ret;
}
Complex Complex:: operator / (const Complex& c)
{
double m = c.a * c.a + c.b * c.b;
double na = (a * c.a + b * c.b) / m;
double nb = (b * c.a - a * c.b) / m;
Complex ret(na, nb);
return ret;
}
bool Complex::operator == (const Complex& c)
{
return a == c.a && b == c.b;
}
bool Complex::operator != (const Complex& c)
{
return !(*this == c);
}
Complex& Complex::operator = (const Complex& c)
{
if (this != &c)
{
a = c.a;
b = c.b;
}
return *this;
}
Complex& Complex::operator ++ ()
{
a = a + 1;
b = b + 1;
return *this;
}
Complex Complex::operator ++ (int)
{
Complex ret(a, b);
a = a + 1;
b = b + 1;
return ret;
}
// 28-3.cpp
#include<iostream>
#include"Complex.h"
using namespace std;
int main()
{
Complex c1(1, 2);
Complex c2 = c1++;
cout << "(" << c1.getA() << "," << c1.getB() << ")" << endl;
cout << "(" << c2.getA() << "," << c2.getB() << ")" << endl;
return 0;
}
在复数类中重载 ++ 操作符,实现前置 ++ 与后置 ++
$ g++ 28-3.cpp Complex.cpp -o 28-3
$ ./28-3
(2,3)
(1,2)
3 小结
1、编译优化使得最终的可执行程序更加高效
2、前置 ++ 和后置 ++ 操作符可以重载
3、++ 操作符重载必须符合其原生语义
4、对于基础类型,前置 ++ 和后置 ++ 效率几乎相同,对于类类型,前置 ++ 效率高于后置 ++