Detailed description of the C ++ explicit keyword Application Method

Source: Internet
Author: User

Description:

In C ++, the explicit keyword is used to modify the constructor of a class. The class of the constructor to be modified cannot undergo corresponding implicit type conversion. type conversion can only be performed in display mode.

Note:

*

The explicit keyword can only be used in the constructor declaration within the class.

*

The explicit keyword acts on the constructor of a single parameter.

* In C ++, the explicit keyword is used to modify the class constructor. The class of the constructor to be modified cannot undergo corresponding implicit type conversion.

Reprinted: http://developer.51cto.com/art/201002/183398.htm

Many important keywords in the C ++ programming language play a very important role in actual programming. The C ++
The explicit keyword is one of the most frequently used keywords. Let's take a look at this knowledge.

The C ++ explicit keyword is used to modify the class constructor, indicating that the constructor is explicit. Since there is an "Explicit", there must be "implicit ", so what is explicit and what is implicit?

If the constructor of the C ++ class has a parameter, a default conversion operation will be performed during compilation: Convert the data of the corresponding data type of the constructor to this type of object, as shown below:

Class myclass {public: myclass (INT num );}
//. Myclass OBJ = 10; // OK, convert int to myclass

In the code above, the compiler automatically converts an integer to a myclass object, which is actually equivalent to the following operation:

Myclass temp (10); myclass OBJ = temp;

The operations related to all the above C ++ explicit it keywords are called "implicit conversions ".

What should we do if we want to avoid this automatic conversion function? Hey, this is the role of the keyword explicit. Declare the class constructor as "Explicit", that is, add the explicit it before declaring the constructor, this will prevent this automatic conversion operation. If we modify the constructor of the above myclass class to be explicit, the following code will not be able to be compiled, as shown below:

Class myclass {public: explicit myclass (INT num );}
//. Myclass OBJ = 10; // err, can't non-explict convert

The preceding section describes the 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.