C++中操作符的前置與后置有什么區(qū)別
一、值得思考的問題
下面的代碼有沒有區(qū)別?為什么?

二、意想不到的事實
- 現(xiàn)代編譯器產品會對代碼進行優(yōu)化
- 優(yōu)化使得最終的二進制程序更加高效
- 優(yōu)化后的二進制程序丟失了 C/C++ 的原生語義
- 不可能從編譯后的二進制程序還原 C/C++ 程序
三、++ 操作符重載
++ 操作符可以重載嗎?如何區(qū)分前置++ 和后置++?
++ 操作符可以被重載
- 全局函數和成員函數均可進行重載
- 重載前置++操作符不需要額外的參數
- 重載后置++操作符需要一個 int 類型的占位參數
下面來看 ++ 操作符重載的示例:
#include <iostream>
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);
Test m(0);
Test tt = t++;
cout << "tt = " << tt.value() << endl;
cout << "t = " << t.value() << endl;
Test mm = ++m;
cout << "mm = " << mm.value() << endl;
cout << "m = " << m.value() << endl;
return 0;
}輸出結果如下:

前置++的效率高于后置++,因為前置的++沒有生成額外的對象,意味著不需要過多的內存,也就是不需要在棧上生成對象。而后置的++需要創(chuàng)建??臻g上的對象,占用??臻g,并且需要調用構造函數,返回后需要調用析構函數。
四、真正的區(qū)別
對于基礎類型的變量
- 前置++的效率與后置++的效率基本相同
- 根據項目組編碼規(guī)范進行選擇
對于類類型的對象
- 前置++的效率高于后置++
- 盡量使用前置++操作符提高程序效率
前面寫過的復數類可以進一步完善了:
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);
};
#endifComplex.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;
}test.cpp:
#include <iostream>
#include "Complex.h"
using namespace std;
int main()
{
Complex a(0, 0);
Complex b(0, 0);
Complex aa = a++;
Complex bb = ++b;
cout << "aa的實部為: " << aa.getA() << endl;
cout << "aa的實部為: " << aa.getB() << endl;
cout << "bb的實部為: " << bb.getA() << endl;
cout << "bb的實部為: " << bb.getB() << endl;
return 0;
}輸出結果如下:

五、小結
- 編譯優(yōu)化使得最終的可執(zhí)行程序更加高效
- 前置++操作符和后置++操作符都可以被重載
- ++操作符的重載必須符合其原生語義
- 對于基礎類型,前置++與后置++的效率幾乎相同
- 對于類類型,前置++的效率高于后置++
到此這篇關于C++中操作符的前置與后置有什么區(qū)別的文章就介紹到這了,更多相關C++操作符內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
C語言實現(xiàn)學生信息管理系統(tǒng)(多文件)
這篇文章主要為大家詳細介紹了C語言實現(xiàn)學生信息管理系統(tǒng),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-12-12

