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:

[CPP]View PlainCopy 
  1. Class CString
  2. {
  3. char* _str;
  4. Public
  5. //Constructors
  6. CString (const char* pstr): _str (nullptr)
  7. {
  8. Updatestring (PSTR);
  9. }
  10. //destructor
  11. ~cstring ()
  12. {
  13. if (_STR)
  14. Free (_STR);
  15. }
  16. Public
  17. void updatestring (const char* pstr) throw ()
  18. {
  19. if (pstr = = nullptr)
  20. return;
  21. if (_STR)
  22. Free (_STR);
  23. _str = (char*) malloc (strlen (PSTR) + 1);
  24. strcpy (_STR,PSTR);
  25. }
  26. Public
  27. char* getstr () const throw ()
  28. {
  29. return _str;
  30. }
  31. };

We can use this:

[CPP]View PlainCopy 
    1. Auto str = std::make_unique<cstring> ("123");
    2. printf (Str->getstr ());

But this is not possible:

[CPP]View PlainCopy 
    1. Auto str = std::make_unique<cstring> (); //failed because there is no parameter constructor


OK, let's use default to:

[CPP]View PlainCopy 
  1. Class CString
  2. {
  3. char* _str = nullptr;
  4. Public
  5. CString () = default;
  6. Public
  7. //Constructors
  8. CString (const char* pstr): _str (nullptr)
  9. {
  10. Updatestring (PSTR);
  11. }
  12. //destructor
  13. ~cstring ()
  14. {
  15. if (_STR)
  16. Free (_STR);
  17. }
  18. Public
  19. void updatestring (const char* pstr) throw ()
  20. {
  21. if (pstr = = nullptr)
  22. return;
  23. if (_STR)
  24. Free (_STR);
  25. _str = (char*) malloc (strlen (PSTR) + 1);
  26. strcpy (_STR,PSTR);
  27. }
  28. Public
  29. char* getstr () const throw ()
  30. {
  31. return _str;
  32. }
  33. };


So we can use it this way:

[CPP]View PlainCopy 
    1. Auto Str_def = std::make_unique<cstring> ();
    2. Str_def->updatestring ("123");
    3. 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  
  1. template<TypeName T>
  2. Class Cstackmemoryalloctor
  3. {
  4. mutable t* _ptr;
  5. Public
  6. explicit Cstackmemoryalloctor (size_t size) throw (): _ptr (nullptr)
  7. {
  8. _ptr = (t*) malloc (size);
  9. }
  10. ~cstackmemoryalloctor ()
  11. {
  12. if (_ptr)
  13. Free (_PTR);
  14. }
  15. Public
  16. Operator t* () const throw ()
  17. {
  18. t* tmp = _PTR;
  19. _ptr = nullptr;
  20. return tmp;
  21. }
  22. Public
  23. t* getptr () const throw ()
  24. {
  25. return _ptr;
  26. }
  27. };


We use this class in this way:

[CPP]View PlainCopy 
    1. cstackmemoryalloctor<wchar_t> str (128);
    2. wchar_t* pstr = str. GetPtr ();
    3. wcscpy (pstr,l"123\n");
    4. wprintf (PSTR);


But others can also use this:

[CPP]View PlainCopy 
    1. 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 
    1. Private
    2. void* operator New (std::size_t)
    3. {
    4. return nullptr;
    5. }

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 
    1. Public
    2. 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

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.