C++中關(guān)于=default和=delete問題
什么是默認(rèn)功能呢(Defaulted Function)
明確默認(rèn)的函數(shù)聲明式一種新的函數(shù)聲明方式,在C++11發(fā)布時做出了更新。C++11允許添加“=default”說明符到函數(shù)聲明的末尾,以將該函數(shù)聲明為顯示默認(rèn)構(gòu)造函數(shù)。
這就使得編譯器為顯示默認(rèn)函數(shù)生成了默認(rèn)實(shí)現(xiàn),它比手動編程函數(shù)更加有效。
例如,每當(dāng)我們聲明一個有參構(gòu)造函數(shù)時,編譯器就不會創(chuàng)建默認(rèn)構(gòu)造函數(shù)。在這種情況下,我們可以使用default說明符來創(chuàng)建默認(rèn)說明符。
以下代碼演示了如何創(chuàng)建:
// use of defaulted functions #include <iostream> using namespace std; class A { public: // A user-defined A(int x){ cout << "This is a parameterized constructor"; } // Using the default specifier to instruct // the compiler to create the default implementation of the constructor. A() = default; }; int main(){ A a; //call A() A x(1); //call A(int x) cout<<endl; return 0; }
在上面的例子中,我們不必指定構(gòu)造函數(shù)A()的主體,因?yàn)橥ㄟ^附加說明符’= default’,編譯器將創(chuàng)建此函數(shù)的默認(rèn)實(shí)現(xiàn)。
使用此“=default”符號有什么限制?
默認(rèn)函數(shù)需要用于特殊的成員函數(shù)(默認(rèn)構(gòu)造函數(shù),復(fù)制構(gòu)造函數(shù),析構(gòu)函數(shù)等),或者沒有默認(rèn)參數(shù)。
例如,以下代碼解釋了非特殊成員函數(shù)不能默認(rèn):
// non-special member functions can't be defaulted(非特殊成員函數(shù)不能使用default) class B { public: // Error, func is not a special member function. int func() = default; // Error, constructor B(int, int) is not a special member function. B(int, int) = default; // Error, constructor B(int=0) has a default argument. B(int = 0) = default; }; int main() { return 0; }
運(yùn)行結(jié)果:
當(dāng)我們可以使用“{}”簡單的空的實(shí)體時,使用’= default’有什么優(yōu)點(diǎn)?
盡管兩者可能表現(xiàn)相同,但使用default而不是使用{}仍然有一定的好處。
以下幾點(diǎn)做了一定的解釋:
1、給用戶定義的構(gòu)造函數(shù),即使它什么也不做,使得類型不是聚合,也不是微不足道的。如果您希望您的類是聚合類型或普通類型(或通過傳遞性,POD類型),那么需要使用’= default’。
2、使用’= default’也可以與復(fù)制構(gòu)造函數(shù)和析構(gòu)函數(shù)一起使用。例如,空拷貝構(gòu)造函數(shù)與默認(rèn)拷貝構(gòu)造函數(shù)(將執(zhí)行其成員的復(fù)制副本)不同。對每個特殊成員函數(shù)統(tǒng)一使用’= default’語法使代碼更容易閱讀。
再來說一下Deleted Function
在C ++ 11之前,操作符delete 只有一個目的,即釋放已動態(tài)分配的內(nèi)存。
而C ++ 11標(biāo)準(zhǔn)引入了此操作符的另一種用法,即:禁用成員函數(shù)的使用。
這是通過附加= delete來完成的; 說明符到該函數(shù)聲明的結(jié)尾。
使用’= delete’說明符禁用其使用的任何成員函數(shù)稱為expicitly deleted函數(shù)。
雖然不限于它們,但這通常是針對隱式函數(shù)。
以下示例展示了此功能派上用場的一些任務(wù):
禁用拷貝構(gòu)造函數(shù)
// copy-constructor using delete operator #include <iostream> using namespace std; class A { public: A(int x): m(x) { } // Delete the copy constructor A(const A&) = delete; // Delete the copy assignment operator A& operator=(const A&) = delete; int m; }; int main() { A a1(1), a2(2), a3(3); // Error, the usage of the copy assignment operator is disabled a1 = a2; // Error, the usage of the copy constructor is disabled a3 = A(a2); return 0; }
禁用不需要的參數(shù)轉(zhuǎn)換
// type conversion using delete operator #include <iostream> using namespace std; class A { public: A(int) {} // Declare the conversion constructor as a deleted function. Without this step, // even though A(double) isn't defined, the A(int) would accept any double value // for it's argumentand convert it to an int A(double) = delete; }; int main() { A A1(1); // Error, conversion from double to class A is disabled. A A2(100.1); return 0; }
請注意,刪除的函數(shù)是隱式內(nèi)聯(lián)的,這一點(diǎn)非常重要。
刪除的函數(shù)定義必須是函數(shù)的第一個聲明。
換句話說,以下方法是將函數(shù)聲明為已刪除的正確方法:
class C { public: C(C& a) = delete; };
但是以下嘗試聲明刪除函數(shù)的方法會產(chǎn)生錯誤:
// incorrect syntax of declaring a member function as deleted class C { public: C(); }; // Error, the deleted definition of function C must be the first declaration of the function. C::C() = delete;
明確刪除函數(shù)有什么好處?
刪除特殊成員函數(shù)提供了一種更簡潔的方法來防止編譯器生成我們不想要的特殊成員函數(shù)。(如“禁用拷貝構(gòu)造函數(shù)”示例中所示)。
刪除正常成員函數(shù)或非成員函數(shù)可防止有問題的類型導(dǎo)致調(diào)用非預(yù)期函數(shù)(如“禁用不需要的參數(shù)轉(zhuǎn)換”示例中所示)。
總結(jié)
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
C++中的函數(shù)指針與函數(shù)對象的總結(jié)
以下是對C++中的函數(shù)指針與函數(shù)對象的使用進(jìn)行了詳細(xì)的分析介紹,需要的朋友可以參考下2013-07-07C++中Socket網(wǎng)絡(luò)編程實(shí)例詳解
這篇文章主要介紹了C++中Socket網(wǎng)絡(luò)編程實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-04-04輸入一個字符串,取出其中的整數(shù)(實(shí)現(xiàn)代碼)
輸入一個字符串,內(nèi)含所有數(shù)字和非數(shù)字字符。將其中連續(xù)的數(shù)字作為一個整數(shù),依次存放到一個數(shù)組中,統(tǒng)計共有多少個整數(shù),并輸出這些數(shù)2013-09-09C++中的new/delete、構(gòu)造/析構(gòu)函數(shù)、dynamic_cast分析
這篇文章主要介紹了C++中的new/delete、構(gòu)造/析構(gòu)函數(shù)、dynamic_cast分析 本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2019-05-05C語言實(shí)現(xiàn)俄羅斯方塊課程設(shè)計
這篇文章主要為大家詳細(xì)介紹了C語言實(shí)現(xiàn)俄羅斯方塊課程設(shè)計,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-06-06