c 常問問題十六 移動建構函式

2022-05-30 17:06:11 字數 924 閱讀 3752

今天我們來講講c++11中引入了兩個新東西

1.move constructor(移動建構函式)

2.

move assignment(移動賦值)

class rule_of_five

~rule_of_five()

rule_of_five(const rule_of_five& other) // copy constructor

rule_of_five(rule_of_five&& other) : cstring(other.cstring) // move constructor

rule_of_five& operator=(const rule_of_five& other) // copy assignment

rule_of_five& operator=(rule_of_five&& other) // move assignment

那麼新加入的移動賦值以及移動拷貝要怎麼使用呢,直接看**

#include #include #include #include int main()

output:

after copy, str is "hello"

after move, str is ""

the contents of the vector are "hello", "hello"

看完大概明白一點兒了,加上move之後,str物件裡面的內容被"移動"到新的物件中並插入到陣列之中了,同時str被清空了。這樣一來省去了物件拷貝的過程。所以說在str物件不再使用的情況下,這種做法的效率更高一些!

Mysql面試常問問題

資料庫基礎知識 1 為什麼要使用資料庫 2 什麼是sql?3 什麼是mysql?4 資料庫三大正規化是什麼 5 mysql有關許可權的表都有哪幾個 6 mysql的binlog有有幾種錄入格式?分別有什麼區別?資料型別 1 mysql有哪些資料型別 引擎1 mysql儲存引擎myisam與innod...

php 面試常問問題

1.看看簡歷,會問一些過去做的專案的使用者量 pv 吞吐量 相關難點和解決方法等 2.資料庫設計經驗,為什麼進行分表?分庫?一般多少資料量開始分表?分庫?分庫分表的目的?什麼是資料庫垂直拆分?水平拆分?分割槽等等?可以舉例說明 3.資料庫優化有哪些?分別需要注意什麼?4.web開發方面會遇到哪些快取...

iOS 面試常問問題總結

1.除了mvc模式你還熟悉什麼設計模式 模式 當乙個類的某些功能需要由別的類實現,但是又不確定具體是哪個類實現的 單例模式 確保程式執行期,某個類只有乙份例項,用於進行資源共享 工廠模式 工廠模式建立的例項,多與proxy模式配合,建立可替代的類,一般用於基礎控制項的建立 觀察者模式 一般為mode...