C + + implicit type conversion and keyword explicit

Source: Internet
Author: User
Tags wrapper
1. What is type conversionThere are related dependencies between some types, and values or objects of related types of that type are used at an operand location that requires a type.
2. What is implicit type conversionConversion rules are automatically executed by the compiler without programmer intervention
3. When an implicit type conversion is generated1 in a mixed-type expression, the operands are converted to the same type. (Two sides of the operator) int ival; int dval; Ival>=dval
2) as a conditional expression is converted to bool type. int ival=3; if (ival);
3 to initialize a variable with an expression or to assign an expression to a variable, the expression is converted to the type of the variable. int val=3.14;//converts 3.14 to an int type int *p=0;//A null pointer that converts int 0 to an int pointer.
4 The implicit type conversion that occurs when the function is called participates in the type of the argument argument and its corresponding parameter type: The argument must have a data type that is the same as the formal parameter type or can be implicitly converted to the parameter type (by the way: function overload determination, that is, function matching, there was an error in writing a wrapper printout function, There is an error in the std::string char* type, now think about the concept of function overload or not.
4. Why do you pull these off?Only look at the explicit keyword, said is very simple, but I have seen two or three times, see Primer, always feel that some things are not enough. But if the reason is actually very simple, explicit is about the class type implicit conversion keyword. Is what is described above for implicit type conversions. Things to see, each is not isolated. Know it, know it.
5.explicit Keywords 1. Generate class type implicit conversion conditionA single parameter calls the constructor to define an implicit conversion from the formal parameter type to the class type. If there is no single formal parameter, or if a single shape participates in the explicit keyword, an implicit conversion is described in 3, and there is no implicit conversion at all. Conversely, an implicit class type conversion is generated when in the case described in 3.
2. How to use explicitConstructors are declared as explicit to prevent the use of constructors where implicit conversions are required. You do not need to add explicit when you define. In the Boost timer class, the constructor for only one parameter uses the explicit, which contains two parameters, the constructor above, not joining the explicit.
3. Class type implicit conversion process [CPP]View Plain copy print?         Class Sales_item {public:sales_item (const std::string & book);     Sales_item (Std::istream & is);          };     BOOL Same_isbn (Sales_item book) {} std::string Null_book = "9-999-99999-9";     ITEM.SAME_ISBN (Null_book); ITEM.SAME_ISBN (CIN);
A single parameter constructor makes it possible to implicitly convert a class type. The Sales_item type is required in the SAME_ISBN function. However, because of the string single parameter constructor, Null_book is implicitly converted to type Sales_item. How do I convert? ITEM.SAME_ISBN (Null_book);
Essentially equals ITEM.SAME_ISBN (Sales_item (Null_book)); The same is true for CIN.
6. A summary of the explicit.

C + + Primer mentioned: Typically, unless there is a clear reason to define an implicit conversion, the parameter constructor should be explicit. Setting the constructor to explicit avoids errors, and when the conversion is useful, the user can explicitly construct the object.

Google's C + + specification mentions that the advantage of explicit is that it avoids outdated type transformations and has no disadvantages. So Google has agreed that all single parameter constructors must be displayed, and that only a handful of the copy constructors can be declared explicit. For example, a class that acts as a transparent wrapper for other classes.

Effective C + + mentions that constructors declared as explicit are often more popular than their non-explicit brothers. Because they prevent the compiler from performing type conversions that are unexpected (and often not expected). Unless I have a good reason to allow constructors to be used for implicit type conversions, I will declare it as explicit. I encourage you to follow the same policy.



Another very good blog:

http://blog.csdn.net/shuangshuang37278752/article/details/14143093

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.