1. 程式人生 > >C++筆記 第四十課 前置操作符和後置操作符---狄泰學院

C++筆記 第四十課 前置操作符和後置操作符---狄泰學院

如果在閱讀過程中發現有錯誤,望評論指正,希望大家一起學習,一起進步。
學習C++編譯環境:Linux

第四十課 前置操作符和後置操作符

1.值得思考的問題

下面的程式碼有沒有區別?為什麼?答案是5.真正的區別
i++; //i的值作為返回值,i自增1
++i; // i自增1,i的值作為返回值

40-1 真的有區別嗎?

#include <iostream>
#include <string>
using namespace std;
int main()
{
    int i = 0;
    i++;
    ++i;
    return 0;
}

2.意想不到的事實

現代編譯器產品會對程式碼進行優化
優化是的最終的二進位制程式更加高效
優化後的二進位制程式丟失了C/C++的原生語義
不可能從編譯後的二進位制程式還原C/C++程式

3.思考

++操作符可以過載嗎?如何區分前置++和後置++?

4.++操作符過載

++操作符可以被過載
全域性函式和成員函式均可進行過載
過載前置++操作符不需要額外的引數
過載後置++操作符需要一個int型別的佔位引數

40-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;
}
執行結果
[email protected]:~/c++/40$ g++ test.cpp Complex.cpp
[email protected]:~/c++/40$ ./a.out
1
1

小結
編譯優化使得最終的可執行程式更加高效
前置++操作符和後置++操作符都可以被過載
++操作符的過載必須符合其原生語義
對於基礎型別,前置++與後置++的效率幾乎相同
對於類型別前置++的效率高於後置++