【C++深度剖析學習總結】 40 前置操作符和後置操作符
作者 CodeAllen ,轉載請註明出處
1.值得思考的問題
下面的代碼有沒有區別?爲什麼? 答案是5.真正的區別
i++; //i的值作爲返回值,i自增1
++i; // i自增1,i的值作爲返回值
實驗1 真的有區別嗎?
#include <iostream>
#include <string>
using namespace std;
int main()
{
int i = 0;
i++;
++i;
return 0;
}
忽略返回值之後,彙編代碼對應是一樣的
int i = 0;
01145E98 mov dword ptr [i],0
i++;
01145E9F mov eax,dword ptr [i]
01145EA2 add eax,1
01145EA5 mov dword ptr [i],eax
++i;
01145EA8 mov eax,dword ptr [i]
01145EAB add eax,1
01145EAE mov dword ptr [i],eax
return 0;
01145EB1 xor eax,eax
2.意想不到的事實
現代編譯器產品會對代碼進行優化
優化是的最終的二進制程序更加高效
優化後的二進制程序丟失了C/C++的原生語義
不可能從編譯後的二進制程序還原C/C++程序
3.思考
++操作符可以重載嗎?如何區分前置++和後置++?
4.++操作符重載
++操作符可以被重載
全局函數和成員函數均可進行重載
重載前置++操作符不需要額外的參數
重載後置++操作符需要一個int類型的佔位參數
實驗2 ++操作符的重載
#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)
{
Test ret(mValue); //先保存值,棧空間
mValue++;
return ret;
}
};
int main()
{
Test t(0);
//下面兩行意義不同,且前置++效率高,不需要構造函數和析構函數
t++;
++t;
return 0;
}
5.真正的區別
對於基礎類型的變量
- 前置++的效率與後置++的效率基本相同,優化之後的彙編是完全一樣的
- 根據項目組編碼規範進行選擇
對於類類型的對象 - 前置++的效率高於後置++
- 儘量使用前置++操作符提供程序效率
class Complex 複數類的進一步改善
test.cpp
#include<iostream>
#include<string>
#include "Complex.h"
using namespace std;
int main()
{
Complex c(0,0);
c++;
cout << c.getA() << endl;
cout << c.getB() << endl;
return 0;
}
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;
b = b + 1;
return *this;
}
Complex Complex::operator ++ (int)
{
Complex ret(a, b);
a = a + 1;
b = b + 1;
return ret;
}
運行結果
lkk@lkk-virtual-machine:~/c++/40$ g++ test.cpp Complex.cpp
lkk@lkk-virtual-machine:~/c++/40$ ./a.out
1
1
小結
編譯優化使得最終的可執行程序更加高效
前置++操作符和後置++操作符都可以被重載
++操作符的重載必須符合其原生語義
對於基礎類型,前置++與後置++的效率幾乎相同
對於類類型,前置++的效率高於後置++