C++中操作符的前置與后置有什么區(qū)別
一、值得思考的問題
下面的代碼有沒有區(qū)別?為什么?
二、意想不到的事實(shí)
- 現(xiàn)代編譯器產(chǎn)品會(huì)對代碼進(jìn)行優(yōu)化
- 優(yōu)化使得最終的二進(jìn)制程序更加高效
- 優(yōu)化后的二進(jìn)制程序丟失了 C/C++ 的原生語義
- 不可能從編譯后的二進(jìn)制程序還原 C/C++ 程序
三、++ 操作符重載
++ 操作符可以重載嗎?如何區(qū)分前置++ 和后置++?
++ 操作符可以被重載
- 全局函數(shù)和成員函數(shù)均可進(jìn)行重載
- 重載前置++操作符不需要額外的參數(shù)
- 重載后置++操作符需要一個(gè) int 類型的占位參數(shù)
下面來看 ++ 操作符重載的示例:
#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; }
輸出結(jié)果如下:
前置++的效率高于后置++,因?yàn)榍爸玫?+沒有生成額外的對象,意味著不需要過多的內(nèi)存,也就是不需要在棧上生成對象。而后置的++需要?jiǎng)?chuàng)建??臻g上的對象,占用棧空間,并且需要調(diào)用構(gòu)造函數(shù),返回后需要調(diào)用析構(gòu)函數(shù)。
四、真正的區(qū)別
對于基礎(chǔ)類型的變量
- 前置++的效率與后置++的效率基本相同
- 根據(jù)項(xiàng)目組編碼規(guī)范進(jìn)行選擇
對于類類型的對象
- 前置++的效率高于后置++
- 盡量使用前置++操作符提高程序效率
前面寫過的復(fù)數(shù)類可以進(jìn)一步完善了:
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; }
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的實(shí)部為: " << aa.getA() << endl; cout << "aa的實(shí)部為: " << aa.getB() << endl; cout << "bb的實(shí)部為: " << bb.getA() << endl; cout << "bb的實(shí)部為: " << bb.getB() << endl; return 0; }
輸出結(jié)果如下:
五、小結(jié)
- 編譯優(yōu)化使得最終的可執(zhí)行程序更加高效
- 前置++操作符和后置++操作符都可以被重載
- ++操作符的重載必須符合其原生語義
- 對于基礎(chǔ)類型,前置++與后置++的效率幾乎相同
- 對于類類型,前置++的效率高于后置++
到此這篇關(guān)于C++中操作符的前置與后置有什么區(qū)別的文章就介紹到這了,更多相關(guān)C++操作符內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Matlab實(shí)現(xiàn)生成箭頭坐標(biāo)軸詳解
這篇文章主要介紹了如何利用Matlab實(shí)現(xiàn)生成箭頭坐標(biāo)軸,為坐標(biāo)軸增添箭頭,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)Matlab有一定幫助,需要的可以參考一下2022-03-03C++中對C語言結(jié)構(gòu)體用法的擴(kuò)充
今天小編就為大家分享一篇關(guān)于C++中對C語言結(jié)構(gòu)體用法的擴(kuò)充,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2018-12-12C語言實(shí)現(xiàn)學(xué)生信息管理系統(tǒng)(多文件)
這篇文章主要為大家詳細(xì)介紹了C語言實(shí)現(xiàn)學(xué)生信息管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-12-12VScode搭建OpenCV環(huán)境的詳細(xì)步驟
用vscode來寫opencv代碼需要自己編譯OpenCV,主要用到MinGW-w64和CMake工具。接下來通過本文給大家介紹VScode搭建OpenCV環(huán)境的相關(guān)知識,需要的朋友可以參考下2021-11-11