日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

C++中操作符的前置與后置有什么區別_C 語言

作者:清風自在?流水潺潺 ? 更新時間: 2022-07-28 編程語言

一、值得思考的問題

下面的代碼有沒有區別?為什么?

二、意想不到的事實

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

三、++ 操作符重載

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

++ 操作符可以被重載

  • 全局函數和成員函數均可進行重載
  • 重載前置++操作符不需要額外的參數
  • 重載后置++操作符需要一個 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;
}

輸出結果如下:

前置++的效率高于后置++,因為前置的++沒有生成額外的對象,意味著不需要過多的內存,也就是不需要在棧上生成對象。而后置的++需要創建棧空間上的對象,占用棧空間,并且需要調用構造函數,返回后需要調用析構函數。

四、真正的區別

對于基礎類型的變量

  • 前置++的效率與后置++的效率基本相同
  • 根據項目組編碼規范進行選擇

對于類類型的對象

  • 前置++的效率高于后置++
  • 盡量使用前置++操作符提高程序效率

前面寫過的復數類可以進一步完善了:

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的實部為: " << aa.getA() << endl;
    cout << "aa的實部為: " << aa.getB() << endl;
    cout << "bb的實部為: " << bb.getA() << endl;
    cout << "bb的實部為: " << bb.getB() << endl;
    return 0;
}

輸出結果如下:

五、小結

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

原文鏈接:https://blog.csdn.net/weixin_43129713/article/details/124550918

欄目分類
最近更新