C++11学习 新特性之 “=default” 、“=delete”

文章目录

1、 =default 和=delete 概述

  • =default、=delete 是C++11的新特性,分别为:显式缺省(告知编译器生成函数默认的缺省版本)和显式删除(告知编译器不生成函数默认的缺省版本)。C++11中引进这两种新特性的目的是为了增强对“类默认函数的控制”,从而让程序员更加精准地去控制默认版本的函数。 其具体的功能和使用方法下面将一一道来。

2、 类与默认函数

  • 在讲解关键字 default和delete 之前,先对类和类的默认函数作下描述与说明,从而加深对这两个关键字的理解与认知。既要知其然,也要知其所以然。C++中,当设计与编写一个类时,若不显著写明,则类会默认提供如下几个函数:
         (1)构造函数
         (2)析构函数
         (3)拷贝构造函数
         (4)拷贝赋值函数(operator=)
         (5)移动构造函数

         以及全局的默认操作符函数:
         (1)operator,
         (2)operator &
         (3)operator &&
         (4)operator *
         (5)operator->
         (6)operator->*
         (7)operator new
         (8)operator delete

         注:若我们在类中实现了这些版本之后,编译器便不会生成其对应的默认函数版本,这时需要我们显式的写上其对应的默认函数版本。

#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;
}
  • 编译报错,提示: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类的无参构造函数,而该默认构造函数在Student类中,我们没有显式的说明。因此,c++编译器在我们提供了该函数实现之后是不会生成与之对应的默认函数版本的。在Student中我们重载了带2个参数的构造函数,但是无参的构造函数,没有提供,因此会报错。

解决办法,在该类别中显示的提供五参数的构造函数

#include <iostream>
using namespace std;

class Student{
    public:
        Student(){} //显示说明Student的无参构造函数
        Student(const int a,const int b)
//        :m_a(a)
//        ,m_b(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;
}

学习心得

  • :m_a(a),m_b(b)是一个赋值的运算
Student(const int a,const int b)
       :m_a(a)
        ,m_b(b){
   
   {
    {

    }

等效于

Student(const int a,const int b)
    {
            m_a = a;
            m_b = b;
    }
  • 问题:以 Student(){} 这样的方式来声明无参数构造函数,会带来一个问题,就是使得 其不再是 POD 类型,因此可能让编译器失去对这样的数据类型的优化功能。这是不希望看到的。因此最好使用 = default来修饰默认构造函数。
#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;
}

3、 使用“=delete”来限制函数生成

  • C++开发中,我们经常需要控制某些函数的生成。在C++11之前,经常的普遍做法是将其声明为类的 private 成员函数,这样若在类外这些这些函数的操作时候,编译器便会报错,从而达到效果。如例2:
#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的拷贝构造函数和拷贝赋值函数都声明为了 private 属性,因此,当在类外使用拷贝构造和拷贝赋值操作值,编译器会报错。虽然能够达到效果,但是不够直观和简洁。对于追求高效以及简洁来说

这样做有2个问题:
     (1)不是最简化;
     (2)对于友元支持不友好

更为简洁直观的方法是使用: =delete

#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;
}

4、 “=default”使用范围

  • "=default"不仅仅局限于类的定义内,也可以用于类的定义外来修饰成员函数,如例3:
#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;
}
  • Student& Student::operator =(const Student& ) = delete;

参考链接

猜你喜欢

转载自blog.csdn.net/CHYabc123456hh/article/details/108876126