C11的新特性实在是太多了,这2个关键字关注的人倒是少了很多,其中有一个原因便是编译器支持得太慢了(VS到VS2013才支持上),不过这2个关键字那真是极为有用的,下面我们来看看。
【default关键字】
首先我们有一个字符串类:
- class CString  
- {  
-     char* _str;  
-   
- public:  
-     
-     CString(const char* pstr) : _str(nullptr)  
-     {  
-         UpdateString(pstr);  
-     }  
-   
-     
-     ~CString()  
-     {  
-         if (_str)  
-             free(_str);  
-     }  
-   
- public:  
-     void UpdateString(const char* pstr) throw()  
-     {  
-         if (pstr == nullptr)  
-             return;  
-   
-         if (_str)  
-             free(_str);  
-   
-         _str = (char*)malloc(strlen(pstr) + 1);  
-         strcpy(_str,pstr);  
-     }  
-   
- public:  
-     char* GetStr() const throw()  
-     {  
-         return _str;  
-     }  
- };  
 
我们可以这样使用:
- auto str = std::make_unique<CString>("123");  
- printf(str->GetStr());  
 
但是这样是不行的:
- auto str = std::make_unique<CString>(); 
 
好,我们用default来:
- class CString  
- {  
-     char* _str = nullptr;  
-       
- public:  
-     CString() = default;  
-   
- public:  
-     
-     CString(const char* pstr) : _str(nullptr)  
-     {  
-         UpdateString(pstr);  
-     }  
-   
-     
-     ~CString()  
-     {  
-         if (_str)  
-             free(_str);  
-     }  
-   
- public:  
-     void UpdateString(const char* pstr) throw()  
-     {  
-         if (pstr == nullptr)  
-             return;  
-   
-         if (_str)  
-             free(_str);  
-   
-         _str = (char*)malloc(strlen(pstr) + 1);  
-         strcpy(_str,pstr);  
-     }  
-   
- public:  
-     char* GetStr() const throw()  
-     {  
-         return _str;  
-     }  
- };  
 
于是我们可以这样使用了:
- auto str_def = std::make_unique<CString>();  
- str_def->UpdateString(“123”);  
- printf(str_def->GetStr() == nullptr ? "None":str_def->GetStr());  
 
【delete关键字】
假设我们有这样一个类,这个类是用于自动申请内存,进行RAII式管理:
(避免麻烦那些什么拷贝构造拷贝赋值移动构造什么的就不写了)
- template<typename T>  
- class CStackMemoryAlloctor  
- {  
-     mutable T* _ptr;  
-   
- public:  
-     explicit CStackMemoryAlloctor(size_t size) throw() : _ptr(nullptr)  
-     {  
-         _ptr = (T*)malloc(size);  
-     }  
-   
-     ~CStackMemoryAlloctor()  
-     {  
-         if (_ptr)  
-             free(_ptr);  
-     }  
-   
- public:  
-     operator T*() const throw()  
-     {  
-         T* tmp = _ptr;  
-         _ptr = nullptr;  
-         return tmp;  
-     }  
-   
- public:  
-     T* GetPtr() const throw()  
-     {  
-         return _ptr;  
-     }  
- };  
 
我们这样使用这个类:
- CStackMemoryAlloctor<wchar_t> str(128);  
- wchar_t* pstr = str.GetPtr();  
- wcscpy(pstr,L"123\n");  
- wprintf(pstr);  
 
但是别人也可以这样使用:
- auto p = std::make_unique<CStackMemoryAlloctor<wchar_t>>(128);  
 
因为这个类依然可以进行默认new,我们不想让人家进行new怎么办,老办法就是这样:
- private:  
-     void* operator new(std::size_t)  
-     {  
-         return nullptr;  
-     }  
 
把new设置为private了,就行了,但是这样如果别人尝试new,那看到的错误提示简直惨不忍睹。。。
于是C11的delete人性化多了:
- public:  
-     void* operator new(std::size_t) = delete;  
 
当尝试new的时候,提示十分友好,这个方法已被删除。
这个delete可以用来删任何你不爽的东西,比如拷贝构造,赋值拷贝什么鸡巴毛的东西。
 
 
 
原文转自:http://blog.csdn.net/a1875566250/article/details/40406883
原作者为 a1875566250. 请尊重原作者版权
 
C++11的default和delete关键字
原文:http://www.cnblogs.com/LonelyEnvoy/p/5897388.html