C++中 =defaule 和 =delete 使用

本文链接:https://blog.csdn.net/lmb1612977696/article/details/80035487
编译器默认为一个类生成的默认函数
默认构造函数
默认析构函数
默认拷贝构造函数
默认赋值函数
移动构造函数
移动拷贝函数
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
};
1
2
3
4
5
6
7
8
9
10
11
=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;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
3. 模板特化:在模板特例化中,可以用delete来过滤一些特定的形参类型
例如:Widget 类中声明了一个模板函数,当进行模板特化时,要求禁止参数为 void* 的函数调用。
1. 按照私有不实现思路,应该是将特例化的函数声明为私有型,如下:

#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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
=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;
}
1
2
3
4
5
6
这样相当于无参的构造函数也是可以使用的。
————————————————
版权声明:本文为CSDN博主「幻想之渔」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/lmb1612977696/article/details/80035487

posted @ 2019-10-15 11:33  剑无影  阅读(127)  评论(0)    收藏  举报