注:博客中内容主要来自《狄泰软件学院》,博客仅当私人笔记使用。
测试环境:Ubuntu 10.10
GCC版本:9.2.0
一、值得思考的问题
1)下面的代码有没有区别?为什么?
i++; //i 的值作为返回值,i自增1。
++i; //i自增1,i的值作为返回值
test.cpp
#include <iostream>
using namespace std;
int main()
{
int i = 0;
//++i += 1; //使用这个正常,打印结果2
i++ += 1; //使用这个报错。error: lvalue required as left operand of assignment
cout << "i = " << i << endl;
return 0;
}
操作:
1) g++ test.cpp -o test.out编译错误:
test.cpp:13:6: error: lvalue required as left operand of assignment
i++ += 1
错误:左值所需的左操作数被赋值(i++是数值,数值不能给数值赋值)
答:
1)对于基础类型的变量
- 前置++的效率与后置++的效率基本相同
- 根据项目组编码规范进行选择
2)对于类类型的对象
- 前置++的效率高于后置++
- 尽量使用前置++操作符提高程序效率
编程实验
真的有区别吗?
40-1.cpp
#include <iostream>
#include <string>
using namespace std;
int main()
{
int i = 0;
i++;
++i;
return 0;
}
VS2013/g++
++i和i++作为单独一行代码时效果一样
查看汇编代码一致
二、意想不到的事实
1)现代编译器产品会对代码进行优化
2)优化使得最终的二进制程序更加高效
3)优化后的二进制程序丢失了C/C++的原生语义
4)不可能从编译后的二进制程序还原C/C++程序
三、思考
++操作符可以重载吗?如何区分前置++和后置++?
四、++操作符重载(--操作符自己来)
1)++操作符可以被重载
- 全局函数和成员函数均可进行重载
- 重载前置++操作符不需要额外的参数
- 重载后置++操作符需要一个int类型的占位参数
编程实验
++操作符的重载
40-2.cpp
#include <iostream>
#include <string>
using namespace std;
class Test
{
int mValue;
public:
Test(int i)
{
mValue = i;
}
int value()
{
return mValue;
}
//返回返回值为引用,表示返回对象本身
Test& operator ++ () //前置++
{
++mValue; //先++
return *this; //再返回
}
//函数返回值为数值(临时对象)
Test operator ++ (int) //后置++,编译器提示,需要占位参数int
{
Test ret(mValue); //保存++之前的mValue值,栈对象ret,多写程序开销
mValue++; //后++
return ret; //先返回,再++
}
};
int main()
{
Test t(0);
t++;
++t;
return 0;
}
操作:
1) g++ 40-2.cpp -o 40-2.out编译正常:
分析:
后置++效率低,因为它需要在栈上创建临时对象,出栈时还需要销毁对象;前置++没有这种复杂操作。
注意:重载的前置++操作符函数返回值为引用类型,因为原生语义中前置++先运行的是变量本身,最后才执行++操作。这里使用引用是为了返回变量本身,而不是数值。
五、真正的区别
1)对于基础类型的变量
- 前置++的效率与后置++的效率基本相同
- 根据项目组编码规范进行选择
2)对于类类型的对象
- 前置++的效率高于后置++
- 尽量使用前置++操作符提高程序效率
编程实验
复数类的进一步完善
Complex.h
#ifndef _COMPLEX_H_
#define _COMPLEX_H_
class Complex
{
double a;
double b;
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);
};
#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 cm = c.a * c.a + c.b * c.b;
double na = (a * c.a + b * c.b) / cm;
double nb = (b * c.a - a * c.b) / cm;
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; //实部+1
b = b + 1; //虚部+1
return *this;
}
Complex Complex::operator ++ (int) //后置++
{
Complex ret(a, b);
a = a + 1;
b = b + 1;
return ret;
}
小结
1)编译优化使得最终的可执行程序更加高效
2)前置++操作符和后置++操作符都可以被重载
3)++操作符的重载必须符合其原生语义
4)对于基础类型,前置++与后置++的效率几乎相同
5)对于类类型,前置++的效率高于后置++