C11 's new features are too many, these 2 keywords are a lot less attention, one of the reasons is that the compiler support is too slow (vs to VS2013 support), but these 2 keywords that is extremely useful, let's look at the following.
"Default keyword"
First we have a string class:
[CPP]View PlainCopy
- Class CString
- {
- char* _str;
- Public
- //Constructors
- CString (const char* pstr): _str (nullptr)
- {
- Updatestring (PSTR);
- }
- //destructor
- ~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;
- }
- };
We can use this:
[CPP]View PlainCopy
- Auto str = std::make_unique<cstring> ("123");
- printf (Str->getstr ());
But this is not possible:
[CPP]View PlainCopy
- Auto str = std::make_unique<cstring> (); //failed because there is no parameter constructor
OK, let's use default to:
[CPP]View PlainCopy
- Class CString
- {
- char* _str = nullptr;
- Public
- CString () = default;
- Public
- //Constructors
- CString (const char* pstr): _str (nullptr)
- {
- Updatestring (PSTR);
- }
- //destructor
- ~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;
- }
- };
So we can use it this way:
[CPP]View PlainCopy
- Auto Str_def = std::make_unique<cstring> ();
- Str_def->updatestring ("123");
- printf (str_def->getstr () = = nullptr? "None": Str_def->getstr ());
The "delete keyword"
Suppose we have a class that is used to automatically request memory for RAII management:
(Avoid trouble with what copy constructs the copy assignment to move the structure or not to write)
[CPP]View PlainCopy
- 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;
- }
- };
We use this class in this way:
[CPP]View PlainCopy
- cstackmemoryalloctor<wchar_t> str (128);
- wchar_t* pstr = str. GetPtr ();
- wcscpy (pstr,l"123\n");
- wprintf (PSTR);
But others can also use this:
[CPP]View PlainCopy
- Auto p = std::make_unique<cstackmemoryalloctor<wchar_t>> (128);
Because this class can still do the default new, we don't want to let people do new things, the old-fashioned way:
[CPP]View PlainCopy
- Private
- void* operator New (std::size_t)
- {
- return nullptr;
- }
It's OK to set new to private, but then if someone tries new, the error you see is simply horrible ...
So C11 's delete is more humane:
[CPP]View PlainCopy
- Public
- void* operator New (std::size_t) = Delete;
When trying new, the prompt is very friendly and the method has been removed.
This delete can be used to delete anything you're not comfortable with, such as copy construction, and assign a copy of what the chicken hair is.
Translated from: http://blog.csdn.net/a1875566250/article/details/40406883
The original author is a1875566250. Please respect the copyright of the original author
Default and delete keywords for c++11