zoukankan      html  css  js  c++  java
  • C++11中 =default 和 =delete 使用

    编译器默认为一个类生成的默认函数

    1. 默认构造函数
    2. 默认析构函数
    3. 默认拷贝构造函数
    4. 默认赋值函数
    5. 移动构造函数
    6. 移动拷贝函数
    class DataOnly {
    public:
        DataOnly ()                  // default constructor
        ~DataOnly ()                 // destructor
    
        DataOnly (const DataOnly & rhs)              // copy constructor
        DataOnly & operator=(const DataOnly & rhs)    // copy assignment operator
    
        DataOnly (const DataOnly && rhs)         // C++11, move constructor
        DataOnly & operator=(DataOnly && rhs)    // C++11, move assignment operator
    };

    =delete

    1. 禁止使用编译器默认生成的函数

    假如上面的几个函数中,不想使用其中某个,可以将其定义为private,或者使用=delete

    #include <iostream>
    using namespace std;
    
    class DataOnly {
    public:
        DataOnly () {}
        ~DataOnly () {}
    
        DataOnly (const DataOnly & rhs) = delete; //禁止使用该函数
        DataOnly & operator=(const DataOnly & rhs) = delete; //禁止使用该函数
    
        DataOnly (const DataOnly && rhs) {}
        DataOnly & operator=(DataOnly && rhs) {}
    };
    
    int main(int argc, char *argv[]) {
        DataOnly data1;
        DataOnly data2(data1); // error: call to deleted constructor of 'DataOnly'
        DataOnly data3 = data1; // error: call to deleted constructor of 'DataOnly'
        return 0;
    }

    2. delete 关键字可用于任何函数,不仅仅局限于类的成员函数

    #include <iostream>
    using namespace std;
    
    class DataOnly {
    public:
        void fun(int a) {}
        void fun(float a) = delete;
    };
    
    int main(int argc, char *argv[]) {
        DataOnly data1;
        data1.fun(1); // OK
        data1.fun(0.5); // error: call to member function 'fun' is ambiguous
        return 0;
    }

    3. 模板特化:在模板特例化中,可以用delete来过滤一些特定的形参类型

    例如:Widget 类中声明了一个模板函数,当进行模板特化时,要求禁止参数为 void* 的函数调用。 
    按照私有不实现思路,应该是将特例化的函数声明为私有型,如下:

    #include <iostream>
    using namespace std;
    
    class Widget {
    public:
        template<typename T>
        void ProcessPointer(T* ptr) { 
            cout << typeid (T).name() << endl;
        }
    private:        
        void ProcessPointer(void*) { 
            cout << "void" << endl;
        }
    };
    
    int main(int argc, char *argv[]) {
        Widget w;
        int* ip = NULL;
        w.ProcessPointer(ip);
    
        void* vp = NULL;
        w.ProcessPointer(vp); //error: 'ProcessPointer' is a private member of 'Widget' w.ProcessPointer(vp);
        return 0;
    }
    1. =delete直接添加在后面就可以解决这个问题

    = defaule

    在程序员重载了自己上面提到的C++编译器默认生成的函数之后,C++编译器将不在生成这些函数的默认形式。 
    但是C++允许我们使用=default来要求编译器生成一个默认函数,如

    struct Point {
        Point()=default;
        Point(int _x, int _y) : x(_x), y(_y) {}
        int x = 0;
        int y = 0;
    }

    这样相当于无参的构造函数也是可以使用的。

    本文转自:https://blog.csdn.net/lmb1612977696/article/details/80035487

  • 相关阅读:
    PAT 1088. Rational Arithmetic
    PAT 1087. All Roads Lead to Rome
    PAT 1086. Tree Traversals Again
    PAT 1085. Perfect Sequence
    PAT 1084. Broken Keyboard
    PAT 1083. List Grades
    PAT 1082. Read Number in Chinese
    求最大公因数
    [转载]Latex文件转成pdf后的字体嵌入问题的解决
    [转载]Matlab有用的小工具小技巧
  • 原文地址:https://www.cnblogs.com/C-Plus-Plus/p/13916966.html
Copyright © 2011-2022 走看看