最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C++11新特性“=default”,“=delete”的使用

 更新時間:2021年05月25日 08:50:13   作者:君子黎  
=default、=delete 是C++11的新特性,分別為:顯式缺省(告知編譯器生成函數(shù)默認的缺省版本)和顯式刪除(告知編譯器不生成函數(shù)默認的缺省版本),本文就來介紹一下如何使用

1、 =default 和=delete 概述

任何事物的出現(xiàn)都必然有著其出現(xiàn)的理由,伴隨著每一個新的概念產(chǎn)生都會帶來一系列的便利和價值。C++在不斷的演變與發(fā)展,與此同時,伴隨著許多新的特性和功能產(chǎn)生。=default、=delete 是C++11的新特性,分別為:顯式缺省(告知編譯器生成函數(shù)默認的缺省版本)和顯式刪除(告知編譯器不生成函數(shù)默認的缺省版本)。C++11中引進這兩種新特性的目的是為了增強對“類默認函數(shù)的控制”,從而讓程序員更加精準(zhǔn)地去控制默認版本的函數(shù)。其具體的功能和使用方法下面將一一道來。

2、 類與默認函數(shù)

在講解關(guān)鍵字 default和delete 之前,先對類和類的默認函數(shù)作下描述與說明,從而加深對這兩個關(guān)鍵字的理解與認知。既要知其然,也要知其所以然。C++中,當(dāng)我們設(shè)計與編寫一個類時,若不顯著寫明,則類會默認為我們提供如下幾個函數(shù):

(1)構(gòu)造函數(shù)
(2)析構(gòu)函數(shù)
(3)拷貝構(gòu)造函數(shù)
(4)拷貝賦值函數(shù)(operator=)
(5)移動構(gòu)造函數(shù)
以及全局的默認操作符函數(shù):
(1)operator,
(2)operator &
(3)operator &&
(4)operator *
(5)operator->
(6)operator->*
(7)operator new
(8)operator delete

注:若我們在類中實現(xiàn)了這些版本之后,編譯器便不會生成其對應(yīng)的默認函數(shù)版本,這時需要我們顯式的寫上其對應(yīng)的默認函數(shù)版本。

如例1所示:

/*************************************************************************
 * File Name: Student.cpp
 * Author:    The answer
 * Function:  Other        
 * Mail:      2412799512@qq.com 
 * Created Time: 2018年07月17日 星期二 23時08分20秒
 ************************************************************************/

#include<iostream>
using namespace std;
class Student
{
public:
    Student(const int a,const int b)
        :m_a(a)
        ,m_b(b)
    {

    }

int getA()const{return m_a;}
int getB()const{return m_b;}
private:
int m_a;
int m_b;
};

int main(int argc,char **argv)
{
Student stu(1,2);
cout<<stu.getA()<<endl; //1
cout<<stu.getB()<<endl; //2

    Student stu1;           //編譯失敗,報錯: no matching function for call to ‘Student::Student()'

return 0;
}

編譯方式:g++ Student.cpp

編譯報錯,提示:Student.cpp: In function ‘int main(int, char**)':
Student.cpp:34:13: error: no matching function for call to ‘Student::Student()'
Student stu1;

例1定義了一個對象stu1,該對象將會使用Student類的無參構(gòu)造函數(shù),而該默認構(gòu)造函數(shù)在Student類中,我們沒有顯式的說明。因此,c++編譯器在我們提供了該函數(shù)實現(xiàn)之后是不會生成與之對應(yīng)的默認函數(shù)版本的。在Student中我們重載了帶2個參數(shù)的構(gòu)造函數(shù),但是無參的構(gòu)造函數(shù),沒有提供,因此會報錯。

解決方式是:在該類中顯式的提供無參構(gòu)造函數(shù),如下:

/*************************************************************************
 * File Name: Student.cpp
 * Author:    The answer
 * Function:  Other        
 * Mail:      2412799512@qq.com 
 * Created Time: 2018年07月17日 星期二 23時08分20秒
 ************************************************************************/

#include<iostream>
using namespace std;
class Student
{
public:
    Student(){}   //顯式說明Student的無參構(gòu)造函數(shù)
    Student(const int a,const int b)
        :m_a(a)
        ,m_b(b)
    {

    }

int getA()const{return m_a;}
int getB()const{return m_b;}
private:
int m_a;
int m_b;
};

int main(int argc,char **argv)
{
Student stu(1,2);
cout<<stu.getA()<<endl; //1
cout<<stu.getB()<<endl; //2

    Student stu1;
return 0;
}

問題:以 Student(){} 這樣的方式來聲明無參數(shù)構(gòu)造函數(shù),會帶來一個問題,就是使得 其不再是 POD 類型,因此可能讓編譯器失去對這樣的數(shù)據(jù)類型的優(yōu)化功能。這是我們不希望看到的。因此最好使用 = default來修飾默認構(gòu)造函數(shù)。

/*************************************************************************
 * File Name: Student.cpp
 * Author:    The answer
 * Function:  Other        
 * Mail:      2412799512@qq.com 
 * Created Time: 2018年07月17日 星期二 23時08分20秒
 ************************************************************************/

#include<iostream>
using namespace std;
class Student
{
public:
    Student() = default;
    Student(const int a,const int b)
        :m_a(a)
        ,m_b(b)
    {

    }

int getA()const{return m_a;}
int getB()const{return m_b;}
private:
int m_a;
int m_b;
};

int main(int argc,char **argv)
{
Student stu(1,2);
cout<<stu.getA()<<endl; //1
cout<<stu.getB()<<endl; //2

    Student stu1;

//使用is_pod模板類可以查看某類型是否屬于POD類型,若為POD類型,則返回1,反之,返回0
std::cout<<is_pod<Student>::value<<std::endl;  //1
return 0;
}

更多關(guān)于is_pod的用法請參考: std::is_pod 。

3、 使用“=delete”來限制函數(shù)生成

C++開發(fā)中,我們經(jīng)常需要控制某些函數(shù)的生成。在C++11之前,我們經(jīng)常的普遍做法是將其聲明為類的 private 成員函數(shù),這樣若在類外這些這些函數(shù)的操作時候,編譯器便會報錯,從而達到效果。如例2:

/*************************************************************************
 * File Name: Student.cpp
 * Author:    The answer
 * Function:  Other        
 * Mail:      2412799512@qq.com 
 * Created Time: 2018年07月17日 星期二 23時08分20秒
 ************************************************************************/

#include<iostream>
using namespace std;
class Student
{
public:
    Student() = default;
    Student(const int a,const int b)
        :m_a(a)
        ,m_b(b)
    {

    }

int getA()const{return m_a;}
int getB()const{return m_b;}

private:
    Student(const Student& );
    Student& operator =(const Student& );

private:
int m_a;
int m_b;
};

int main(int argc,char **argv)
{
Student stu(1,2);
cout<<stu.getA()<<endl; //1
cout<<stu.getB()<<endl; //2

//Student stu1(stu);
//報錯:Student.cpp:26:5: error: ‘Student::Student(const Student&)' is private


//Student stu1(3,4);
//stu1 = stu;
//報錯:Student.cpp:27:14: error: ‘Student& Student::operator=(const Student&)' is private

std::cout<<is_pod<Student>::value<<std::endl;  //
return 0;
}

例2代碼編譯報錯,因為在類中,我們將Student的拷貝構(gòu)造函數(shù)和拷貝賦值函數(shù)都聲明為了 private 屬性,因此,當(dāng)在類外使用拷貝構(gòu)造和拷貝賦值操作值,編譯器會報錯。雖然能夠達到效果,但是不夠直觀和簡潔。對于追求高效以及簡潔來說,這樣做有2個問題:

(1)不是最簡化;
(2)對于友元支持不友好.

更為簡潔直觀的方法是使用:=delete

/************************************************************************
 * Author:    The answer
 * Function:  Other        
 * Mail:      2412799512@qq.com 
 * Created Time: 2018年07月17日 星期二 23時08分20秒
 ************************************************************************/

#include<iostream>
using namespace std;
class Student
{
public:
    Student() = default;
    Student(const int a,const int b)
        :m_a(a)
        ,m_b(b)
    {

    }

int getA()const{return m_a;}
int getB()const{return m_b;}

    Student(const Student& ) = delete;
    Student& operator =(const Student& ) = delete;

private:
int m_a;
int m_b;
};

int main(int argc,char **argv)
{
Student stu(1,2);
cout<<stu.getA()<<endl; //1
cout<<stu.getB()<<endl; //2

//Student stu1(stu);
//報錯:Student.cpp:39:21: error: use of deleted function ‘Student::Student(const Student&)'

//Student(const Student& );

//Student stu1(3,4);
//stu1 = stu;
//報錯:SStudent.cpp:44:10: error: use of deleted function ‘Student& Student::operator=(const Student&)'

std::cout<<is_pod<Student>::value<<std::endl;  //
return 0;
}

注:若缺省版本被刪除了,重載該函數(shù)是非法的.

4、 “=default”使用范圍

"=default"不僅僅局限于類的定義內(nèi),也可以用于類的定義外來修飾成員函數(shù),如例3:
/*************************************************************************
 * File Name: Student.cpp
 * Author:    The answer
 * Function:  Other        
 * Mail:      2412799512@qq.com 
 * Created Time: 2018年07月17日 星期二 23時08分20秒
 ************************************************************************/

#include<iostream>
using namespace std;
class Student
{
public:
    Student() = default;
    Student(const int a,const int b)
        :m_a(a)
        ,m_b(b)
    {

    }

int getA()const{return m_a;}
int getB()const{return m_b;}

    Student(const Student& ) = delete;
    Student& operator=(const Student& );

private:
int m_a;
int m_b;
};

 Student& Student::operator =(const Student& ) = delete;

int main(int argc,char **argv)
{
Student stu(1,2);
cout<<stu.getA()<<endl; //1
cout<<stu.getB()<<endl; //2

Student stu1(3,4);
    stu1 = stu;
//編譯報錯:Student.cpp:42:10: error: use of deleted function ‘Student& Student::operator=(const Student&)'

std::cout<<is_pod<Student>::value<<std::endl;  //
return 0;
}

到此這篇關(guān)于C++11新特性“=default”,“=delete”的使用的文章就介紹到這了,更多相關(guān)C++11 =default =delete內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家! 

相關(guān)文章

  • C++中String類常見題目分享

    C++中String類常見題目分享

    這篇文章主要為大家詳細介紹了一些C++中String類的常見題目,文中的示例代碼講解詳細,對我們掌握C++有一定的幫助,感興趣的小伙伴可以了解一下
    2023-06-06
  • C++面試八股文之如何避免死鎖詳解

    C++面試八股文之如何避免死鎖詳解

    在C++中,鎖(Lock)是一種同步工具,用于保護共享資源,防止多個線程同時訪問,那么如何避免死鎖的情況出現(xiàn)呢,下面就為大家簡單講講
    2023-07-07
  • C語言中實現(xiàn)KMP算法的實例講解

    C語言中實現(xiàn)KMP算法的實例講解

    KMP算法即字符串匹配算法,C語言中KMP可以避免指針回溯從而達到高效,接下來就來總結(jié)一下C語言中實現(xiàn)KMP算法的實例講解
    2016-06-06
  • 淺談C++ 設(shè)計模式的基本原則

    淺談C++ 設(shè)計模式的基本原則

    這篇文章主要介紹了++ 設(shè)計模式的基本原則,主要的目標(biāo)是實現(xiàn)最終目的,高內(nèi)聚,低耦合,開放封閉原則類的改動是通過增加代碼進行的,感興趣的小伙伴可參考下面文章的具體內(nèi)容
    2021-09-09
  • 淺析C++?atomic?和?memory?ordering

    淺析C++?atomic?和?memory?ordering

    這篇文章主要介紹了C++?atomic?和?memory?ordering的相關(guān)知識,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-04-04
  • C++移動語義詳細介紹使用

    C++移動語義詳細介紹使用

    首先,移動語義和完美轉(zhuǎn)發(fā)這兩個概念是在C++的模板編程的基礎(chǔ)上,新增的特性,主要是配合模板來使用。本篇會從C++的值類型,到移動拷貝與移動賦值來理解移動語義與完美轉(zhuǎn)發(fā)
    2023-01-01
  • wxWidgets實現(xiàn)無標(biāo)題欄窗口拖動效果

    wxWidgets實現(xiàn)無標(biāo)題欄窗口拖動效果

    這篇文章主要為大家詳細介紹了wxWidgets實現(xiàn)無標(biāo)題欄窗口拖動效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-02-02
  • c++中拷貝構(gòu)造函數(shù)的參數(shù)類型必須是引用

    c++中拷貝構(gòu)造函數(shù)的參數(shù)類型必須是引用

    如果拷貝構(gòu)造函數(shù)中的參數(shù)不是一個引用,即形如CClass(const CClass c_class),那么就相當(dāng)于采用了傳值的方式(pass-by-value),而傳值的方式會調(diào)用該類的拷貝構(gòu)造函數(shù),從而造成無窮遞歸地調(diào)用拷貝構(gòu)造函數(shù)。因此拷貝構(gòu)造函數(shù)的參數(shù)必須是一個引用
    2013-07-07
  • OpenCV 2.4.3 C++ 平滑處理分析

    OpenCV 2.4.3 C++ 平滑處理分析

    平滑也稱模糊, 是一項簡單且使用頻率很高的圖像處理方法,本文將詳細介紹OpenCV 2.4+ C++ 平滑處理,需要了解更多的朋友可以詳細參考下
    2012-11-11
  • 使用c++實現(xiàn)OpenCV繪制圓端矩形

    使用c++實現(xiàn)OpenCV繪制圓端矩形

    這篇文章主要介紹了使用c++實現(xiàn)OpenCV繪制圓端矩形,其中著重的講解了OpenCV使用過程中需要注意的一些小細節(jié),避免浪費大家在開發(fā)過程中浪費多余的時間
    2021-08-08

最新評論

芦山县| 淮阳县| 台山市| 都江堰市| 星子县| 石狮市| 象山县| 察哈| 稷山县| 洞口县| 扶余县| 斗六市| 县级市| 老河口市| 肃宁县| 米泉市| 新营市| 新宾| 泉州市| 鲁山县| 锦州市| 邹平县| 凤翔县| 开化县| 仁化县| 伊宁市| 小金县| 定兴县| 岱山县| 土默特右旗| 五莲县| 浦北县| 台东市| 府谷县| 葫芦岛市| 射洪县| 龙里县| 瓮安县| 贵德县| 锦州市| 镇坪县|