C + + Explicit keywords

Source: Internet
Author: User

Translated from: http://www.cnblogs.com/ymy124/p/3632634.html

First, the explicit keyword in C + + can only be used to decorate a class constructor that has only one argument, which is to show that the constructor is displayed, not implicitly, and that another keyword corresponding to it is implicit, meaning hidden, The class constructor is declared by default as implicit (implicit).

So what's the difference between a declared constructor and an implicit declaration? Let's take a look at the following example:

  1. Class Cxstring //is not declared with the explicit keyword, which is implicitly declared by default
  2. {
  3. Public
  4. Char *_pstr;
  5. int _size;
  6. cxstring (int size)
  7. {
  8. _size = size; //The preset size of string
  9. _PSTR = malloc (size + 1); //Allocate a string of memory
  10. memset (_pstr, 0, size + 1);
  11. }
  12. Cxstring (const char *p)
  13. {
  14. int size = strlen (p);
  15. _PSTR = malloc (size + 1); //Allocate a string of memory
  16. strcpy (_PSTR, p); //Copy string
  17. _size = strlen (_PSTR);
  18. }
  19. //destructors are not discussed here, omitted ...
  20. };
  21. //The following is called:
  22. Cxstring string1 (24); //This is OK, pre-allocating 24 bytes of memory for cxstring
  23. cxstring string2 = 10; //This is OK, pre-allocating 10 bytes of memory for cxstring
  24. Cxstring String3; //This is not possible, because there is no default constructor, the error is: "cxstring": there is no appropriate default constructor available
  25. Cxstring String4 ("AAAA"); //This is OK.
  26. cxstring string5 = "BBB"; //This is also OK, the call is cxstring (const char *p)
  27. cxstring String6 = ' C '; //This is OK, in fact, the call is cxstring (int size), and size equals ' C ' ASCII code
  28. string1 = 2; //This is OK, pre-allocating 2 bytes of memory for cxstring
  29. string2 = 3; //This is OK, pre-allocating 3 bytes of memory for cxstring
  30. String3 = string1; //This is OK, at least compile is not a problem, but if the destructor with free to release _pstr memory pointers may be error, the complete code must overload operator "=", and in which to handle memory release

In the above code, "cxstring string2 = 10;" Why is this sentence possible? In C + +, if the constructor has only one parameter, there is a default conversion operation at compile time: Convert the data of that constructor's data type to that class object. That is, "cxstring string2 = 10;" This code, the compiler automatically converts an integral type to a Cxstring class object, which is actually equivalent to the following operation:

    1. Cxstring string2 (10);
    2. Or
    3. Cxstring Temp (10);
    4. cxstring string2 = temp;

However, the _size in the above code represents the size of the string memory allocation, then the second sentence of the call "cxstring string2 = 10;" And the sixth sentence "Cxstring string6 = ' C ';" is not a fish and fish, and it is easy to confuse people. Is there any way to stop this usage? The answer is to use the explicit keyword. Let's change the code above, as follows:

  1. Class cxstring //Use keyword explicit to display the conversion
  2. {
  3. Public
  4. Char *_pstr;
  5. int _size;
  6. explicit cxstring (int size)
  7. {
  8. _size = size;
  9. //code ibid., omitted ...
  10. }
  11. Cxstring (const char *p)
  12. {
  13. //code ibid., omitted ...
  14. }
  15. };
  16. //The following is called:
  17. Cxstring string1 (24); //This is OK.
  18. cxstring string2 = 10; //This is not possible because the explicit keyword cancels the implicit conversion
  19. Cxstring String3; //This is not possible, because there is no default constructor
  20. Cxstring String4 ("AAAA"); //This is OK.
  21. cxstring string5 = "BBB"; //This is also OK, the call is cxstring (const char *p)
  22. cxstring String6 = ' C '; //This is not possible, in fact, the call is cxstring (int size), and size equals ' C ' ASCII code, but the explicit keyword de-implicit conversion
  23. string1 = 2; //This is also not possible, because the implicit conversion is canceled
  24. string2 = 3; //This is also not possible, because the implicit conversion is canceled
  25. String3 = string1; //This is also not possible, because the implicit conversion is canceled, unless the class implements an overload of the operator "="

The purpose of the explicit keyword is to prevent implicit automatic conversion of class constructors.

As already mentioned, the explicit keyword is valid only for a class constructor with one argument, and if the class constructor parameter is greater than or equal to two, the implicit conversion is not generated, so the explicit keyword is not valid. For example:

  1. Class Cxstring //Explicit keyword is invalid if the class constructor parameter is greater than or equal to two
  2. {
  3. Public
  4. Char *_pstr;
  5. int _age;
  6. int _size;
  7. explicit cxstring (int age, int size)
  8. {
  9. _age = age;
  10. _size = size;
  11. //code ibid., omitted ...
  12. }
  13. Cxstring (const char *p)
  14. {
  15. //code ibid., omitted ...
  16. }
  17. };
  18. //This time there are no explicit keywords are the same

However, there is one exception, when the explicit keyword is still valid when there are default values for parameters other than the first argument, when the constructor is called only one parameter is equivalent to the class constructor with only one argument, as in the following example:

  1. Class cxstring //Use keyword Explicit declaration
  2. {
  3. Public
  4. int _age;
  5. int _size;
  6. explicit cxstring (int age, int size = 0)
  7. {
  8. _age = age;
  9. _size = size;
  10. //code ibid., omitted ...
  11. }
  12. Cxstring (const char *p)
  13. {
  14. //code ibid., omitted ...
  15. }
  16. };
  17. //The following is called:
  18. Cxstring string1 (24); //This is OK.
  19. cxstring string2 = 10; //This is not possible because the explicit keyword cancels the implicit conversion
  20. Cxstring String3; //This is not possible, because there is no default constructor
  21. string1 = 2; //This is also not possible, because the implicit conversion is canceled
  22. string2 = 3; //This is also not possible, because the implicit conversion is canceled
  23. String3 = string1; //This is also not possible, because the implicit conversion is canceled, unless the class implements an overload of the operator "="

The above is a detailed introduction to the C + + explicit keyword

C + + Explicit keywords

Related Article

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.