1.值得思考的问题
下面的代码有没有区别?为什么?
i++; //i作为返回值,再i+1
++i; // i自增1,再返回i
由于编译器的优化,i++和++i的底层代码都是一样的,所以在工程上没有区别
汇编的结果:
i++;
000920DC mov eax,dword ptr [i]
000920DF add eax,1
000920E2 mov dword ptr [i],eax
++i;
000920E5 mov eax,dword ptr [i]
000920E8 add eax,1
000920EB mov dword ptr [i],eax
意想不到的事实:
现代编译器产品会对代码进行优化
优化使得最终的二进制程序更加高效
优化后的二进制程序丢失了c/c++的原生语义
不可能从编译后的二进制程序还原c/c++程序
思考:++操作符可以重载吗?如何区分前置和后置++????
2.++操作符重载
++操作符可以被重载
—-全局函数和成员函数均可进行重载
—-重载前置++操作符不需要额外的参数
—-重载后置++操作符需要一个int类型的占位参数
#include <iostream>#include <string>using namespace std;class Test{int mValue;public:Test(int i){mValue = i;}int value(){return mValue;}Test& operator ++ () //返回的是自身{++(this->mValue);return *this;}Test operator ++ (int) //返回值是对象,遵循c语言中后置操作符的原生语义{Test ret(this->mValue); //将当前对象的值保存this->mValue++;return ret;}};int main(){Test t0(0);cout << t0.value() << endl; //0Test tt = ++t0;cout << t0.value() << endl; //1cout << tt.value() << endl; //1Test tx = t0++;cout << tx.value() << endl; //1cout << t0.value() << endl; //2Test t;++t;t++; //两者不一样,前置的++效率更高return 0;}
所以:i++和++i有没有区别?
对于基础类型的变量:
—-前置++的效率与后置++的效率基本相同
—-根据项目编码规范进行选择
对于类类型对象
—-前置++的效率高于后置++
—-尽力使用前置++操作符提供程序的效率
完善的复数类:
#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#include"complex.h"#include"math.h"Complex::Complex(double a,double b)//default only exist at deleration{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 = this->a + c.a;double nb = this->b + c.b;Complex ret(na,nb);return ret;}Complex Complex::operator -(const Complex& c){double na = this->a - c.a;double nb = this->b - c.b;Complex ret(na,nb);return ret;}Complex Complex::operator *(const Complex& c){double na = (this->a * c.a) - (this->b * c.b);double nb = (this->a * c.b) + (this->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 = (this->a * c.a) + (this->b * c.b) / cm;double nb = (this->b * c.a) - (this->a * c.b) / cm;Complex ret(na,nb);return ret;}bool Complex::operator ==(const Complex& c){return (this->a == c.a)&& (this->b == c.b);}bool Complex::operator !=(const Complex& c){return !(*this == c);}Complex& Complex::operator =(const Complex& c){if(this !=&c){this->a = c.a;this->b = c.b;}return *this; //return current}Complex& Complex::operator ++(){this->a = this->a + 1;this->b = this->b + 1;return *this;}Complex Complex::operator ++(int){Complex ret(a,b);this->a = this->a + 1;this->b = this->b + 1;return ret;}//main.c#include <iostream>#include <string>#include "complex.h"using namespace std;int main(){Complex c(0,0);Complex b = c++;cout << c.getA() << endl;cout << c.getB() << endl;cout << b.getA() << endl;cout << b.getB() << endl;return 0;}
小结:
1.编译优化使得最终的可执行程序更加高效
2.前置++操作符和后置++操作符都可以被重载
3.++操作符的重载必须符合其原生语义
4.对于基础类型,前置++与后置++的效率几乎相同
5.对于类类型,前置++的效率高于后置++
