溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

C++11中新特性“=default”,“=delete”如何使用

發(fā)布時(shí)間:2021-05-25 14:14:29 來(lái)源:億速云 閱讀:259 作者:小新 欄目:開(kāi)發(fā)技術(shù)

小編給大家分享一下C++11中新特性“=default”,“=delete”如何使用,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

1、 =default 和=delete 概述

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

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

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

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

注:若我們?cè)陬愔袑?shí)現(xiàn)了這些版本之后,編譯器便不會(huì)生成其對(duì)應(yīng)的默認(rèn)函數(shù)版本,這時(shí)需要我們顯式的寫(xiě)上其對(duì)應(yīng)的默認(rèn)函數(shù)版本。

如例1所示:

/*************************************************************************
 * File Name: Student.cpp
 * Author:    The answer
 * Function:  Other        
 * Mail:      2412799512@qq.com 
 * Created Time: 2018年07月17日 星期二 23時(shí)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;           //編譯失敗,報(bào)錯(cuò): no matching function for call to ‘Student::Student()'

return 0;
}

編譯方式:g++ Student.cpp

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

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

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

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

#include<iostream>
using namespace std;
class Student
{
public:
    Student(){}   //顯式說(shuō)明Student的無(wú)參構(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;
}

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

/*************************************************************************
 * File Name: Student.cpp
 * Author:    The answer
 * Function:  Other        
 * Mail:      2412799512@qq.com 
 * Created Time: 2018年07月17日 星期二 23時(shí)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的用法請(qǐng)參考: std::is_pod 。

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

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

/*************************************************************************
 * File Name: Student.cpp
 * Author:    The answer
 * Function:  Other        
 * Mail:      2412799512@qq.com 
 * Created Time: 2018年07月17日 星期二 23時(shí)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);
//報(bào)錯(cuò):Student.cpp:26:5: error: ‘Student::Student(const Student&)' is private


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

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

例2代碼編譯報(bào)錯(cuò),因?yàn)樵陬愔?,我們將Student的拷貝構(gòu)造函數(shù)和拷貝賦值函數(shù)都聲明為了 private 屬性,因此,當(dāng)在類外使用拷貝構(gòu)造和拷貝賦值操作值,編譯器會(huì)報(bào)錯(cuò)。雖然能夠達(dá)到效果,但是不夠直觀和簡(jiǎn)潔。對(duì)于追求高效以及簡(jiǎn)潔來(lái)說(shuō),這樣做有2個(gè)問(wèn)題:

(1)不是最簡(jiǎn)化;
(2)對(duì)于友元支持不友好.

更為簡(jiǎn)潔直觀的方法是使用:=delete

/************************************************************************
 * Author:    The answer
 * Function:  Other        
 * Mail:      2412799512@qq.com 
 * Created Time: 2018年07月17日 星期二 23時(shí)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);
//報(bào)錯(cuò):Student.cpp:39:21: error: use of deleted function ‘Student::Student(const Student&)'

//Student(const Student& );

//Student stu1(3,4);
//stu1 = stu;
//報(bào)錯(cuò):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),也可以用于類的定義外來(lái)修飾成員函數(shù),如例3:
/*************************************************************************
 * File Name: Student.cpp
 * Author:    The answer
 * Function:  Other        
 * Mail:      2412799512@qq.com 
 * Created Time: 2018年07月17日 星期二 23時(shí)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;
//編譯報(bào)錯(cuò):Student.cpp:42:10: error: use of deleted function ‘Student& Student::operator=(const Student&)'

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

以上是“C++11中新特性“=default”,“=delete”如何使用”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

c++
AI