C++11中delete的使用

出处:http://blog.csdn.net/fengbingchun/article/details/52475108

In C++11, defaulted and deleted functions give you explicit control over whether the special member functions are automatically generated.

The opposite of a defaulted function is a deleted function.

Deleted functions are useful for preventing object copying, among the rest. Recall that C++ automatically declares a copy constructor and an assignment operator for classes. To disable copying, declare these two special member functions “=delete”.

The delete specifier can also be used to make sure member functions with particular parameters aren’t called.

=default: it means that you want to use the compiler-generated version of that function, so you don't need to specify a body.

=delete: it means that you don't want the compiler to generate that function automatically.

C++11中,对于deleted函数,编译器会对其禁用,从而避免某些非法的函数调用或者类型转换,从而提高代码的安全性。

对于 C++ 的类,如果程序员没有为其定义特殊成员函数,那么在需要用到某个特殊成员函数的时候,编译器会隐式的自动生成一个默认的特殊成员函数,比如默认的构造函数、析构函数、拷贝构造函数以及拷贝赋值运算符。

为了能够让程序员显式的禁用某个函数,C++11标准引入了一个新特性:deleted函数。程序员只需在函数声明后加上”=delete;”,就可将该函数禁用。

deleted函数特性还可用于禁用类的某些转换构造函数,从而避免不期望的类型转换。

deleted函数特性还可以用来禁用某些用户自定义的类的new操作符,从而避免在自由存储区创建类的对象。

必须在函数第一次声明的时候将其声明为deleted函数,否则编译器会报错。即对于类的成员函数而言,deleted函数必须在类体里(inline)定义,而不能在类体外(out-of-line)定义。

虽然defaulted函数特性规定了只有类的特殊成员函数才能被声明为defaulted函数,但是deleted函数特性并没有此限制。非类的成员函数,即普通函数也可以被声明为deleted函数。

下面是从其他文章中copy的测试代码,详细内容介绍可以参考对应的reference:

  1. #include "delete.hpp"  
  2. #include <iostream>  
  3.   
  4. //  
  5. // reference: http://www.learncpp.com/cpp-tutorial/b-6-new-virtual-function-controls-override-final-default-and-delete/  
  6. class Foo  
  7. {  
  8.     Foo& operator=(const Foo&) = delete// disallow use of assignment operator  
  9.     Foo(const Foo&) = delete// disallow copy construction  
  10. };  
  11.   
  12. class Foo_1  
  13. {  
  14.     Foo_1(long long); // Can create Foo() with a long long  
  15.     Foo_1(long) = delete// But can't create it with anything smaller  
  16. };  
  17.   
  18. class Foo_2  
  19. {  
  20.     Foo_2(long long); // Can create Foo() with a long long  
  21.     template<typename T> Foo_2(T) = delete// But can't create it with anything else  
  22. };  
  23.   
  24. ///  
  25. // reference: http://www.bogotobogo.com/cplusplus/C11/C11_default_delete_specifier.php  
  26. class A_  
  27. {  
  28. public:  
  29.     A_(int a){};  
  30.     A_(double) = delete;         // conversion disabled  
  31.     A_& operator=(const A_&) = delete;  // assignment operator disabled  
  32. };  
  33.   
  34. int test_delete1()  
  35. {  
  36.     A_ a_(10);     // OK  
  37.     // A_ b(3.14);   // Error: conversion from double to int disabled  
  38.     // a = b;       // Error: assignment operator disabled  
  39.   
  40.     return 0;  
  41. }  
  42.   
  43.   
  44. // reference: https://msdn.microsoft.com/zh-cn/library/dn457344.aspx  
  45. struct widget  
  46. {  
  47.     // deleted operator new prevents widget from being dynamically allocated.  
  48.     void* operator new(std::size_t) = delete;  
  49. };  

GitHubhttps://github.com/fengbingchun/Messy_Test
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值