網站首頁 編程語言 正文
一、值得思考的問題
下面的代碼有沒有區別?為什么?
二、意想不到的事實
- 現代編譯器產品會對代碼進行優化
- 優化使得最終的二進制程序更加高效
- 優化后的二進制程序丟失了 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
相關推薦
- 2022-06-20 C語言手把手帶你掌握帶頭雙向循環鏈表_C 語言
- 2022-03-05 Flutter基本組件Basics?Widget學習_Android
- 2023-04-18 獲取Android簽名MD5的方式實例詳解_Android
- 2022-08-08 python中Pytest常用的插件_python
- 2022-04-20 聊一聊數據請求中Ajax、Fetch及Axios的區別_AJAX相關
- 2023-07-06 mybatis-plus 分頁查詢出現count()而不是count(*)
- 2022-08-15 棧(Stack)和隊列(Queue)的基本操作
- 2022-03-29 Qt超時鎖屏的實現示例_C 語言
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支