Default and delete keywords for c++11

Source: Internet
Author: User

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:
Class cstring{    char* _str;public:    //constructor    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:
Auto str = std::make_unique<cstring> ("123");p rintf (Str->getstr ());
But this is not possible:
Auto str = std::make_unique<cstring> (); Failed because there is no argument constructor

OK, let's use default to:
Class cstring{    char* _str = nullptr;    Public:    CString () = Default;public:    //constructor    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:
Auto Str_def = std::make_unique<cstring> (); Str_def->updatestring ("123");p rintf (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)
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:
Cstackmemoryalloctor<wchar_t> str (+); wchar_t* pstr = str. GetPtr (); wcscpy (pstr,l "123\n"); wprintf (PSTR);

But others can also use this:
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:
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:
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.

Default and delete keywords for c++11

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.