In-depth understanding of Explicitkeyword in C + +

Source: Internet
Author: User

in-depth understanding of Explicitkeyword in C + +

[Email protected]

Http://blog.csdn.net/kezunhai


The Explicitkeyword in C + + can only be used to modify a class constructor that has only one parameter , and its function is to show that the constructor is displayed, not implicitly, Corresponding to it is also a keyword is implicit, meaning is hidden, the class constructor is declared by default as implicit (implicit).

About Explicitkeyword. Let's look at the explanations on MSDN:

This keyword is a declaration specifier, can only beapplied to In-classconstructor declarations. An explicit constructor cannot take part in implicit conversions. The It can only is used to explicitly construct an object.

From the above explanations can be seen. Explicitkeyword: Suppresses the implicit invocation of a single-parameter constructor within a class, which includes, for example, the following three-layer meanings:

(1) The keyword can only be used to modify the constructor inside the class

(2) Prohibit implicit invocation of copy constructors

(3) Prohibit implicit conversions between class objects

first, take a look at implicit conversions, such as the following example:

Class Cexplict{public:cexplict (); Cexplict (bool _explicit) {this->is_explict = _explicit;} Cexplict (const cexplict& Other) {this->is_explict = other.is_explict;} friend void Printexplicit (const cexplict& CX);p rivate:bool is_explict;}; void printexplicit (const cexplict& CX) {cout<< "is_explict=" <<CX.IS_EXPLICT<<ENDL;} int main (int argc, char* argv[]) {cexplict cx1 = true; Cexplict cx2 = cx1;printexplicit (cx1);p rintexplicit (cx2);p rintexplicit (false); GetChar (); return 1;}
In the code above:

Cexplict cx1 = true; Cexplict cx2 = Cx1;printexplicit (false);
Implicitly invokes the single-parameter constructor of the Cexplict class.

Such a call is agreed in the C + + syntax. However, expressions such as cexplict cx1 = True and Printexplicit (false) are very awkward to look at. It is also very confusing to assign the value of a bool type to the cx1 of a cexplicit class. Make your code less readable.

So. To disallow implicit invocation of a single-parameter constructor for a class. C + + introduced the keywordexplicit.

In the definition of a class, the implicit invocation of the constructor can be suppressed in the case of a single-parameter constructor with Money plus Explicitkeyword. For example, the following:

Class Cexplict{public:cexplict (); explicit cexplict (bool _explicit) {this->is_explict = _explicit;} Cexplict (const cexplict& Other) {this->is_explict = other.is_explict;} friend void Printexplicit (const cexplict& CX);p rivate:bool is_explict;}; void printexplicit (const cexplict& CX) {cout<< "is_explict=" <<CX.IS_EXPLICT<<ENDL;} int main (int argc, char* argv[]) {cexplict cx1 = true; Cexplict cx2 = cx1;printexplicit (cx1);p rintexplicit (cx2);p rintexplicit (false); GetChar (); return 1;}
at this point, when the code above is called, it is reported:Error C2440: "Initialize": Cannot convert from "bool" to "cexplict" errors, in order for the program to execute correctly, you need to change the code inside the main function to:

int main (int argc, char* argv[]) {cexplict cx1 (true); Cexplict cx2 (cx1);p rintexplicit (cx1);p rintexplicit (cx2);p rintexplicit (Cexplict (false)); GetChar (); return 1;}

At this point, the program will be able to execute normally, and further add the readability of the program.

Summary:

(1) Explicitkeyword only need to be used in front of a single-parameter constructor within a class.

Because a constructor with no parameters and a multi-parameter constructor always shows the call, it is not meaningful to add explicit before the constructor.

(2) False assumptions prohibit class A objects from being implicitly converted to Class B objects, and you can use Keywordexplicit in Class B, which defines the conversion constructor

        Explicit B (A a) {}explicit B (const a &a) {}
Google's C + + specification mentions that explicit's strengths are the ability to avoid outdated types of transformations, with no drawbacks.

So Google agreed that all the single-parameter constructors must be displayed, and in rare cases the copy constructor can not declare the explicit. For example, a class that is a transparent wrapper for other classes.


Effective C + + says: Constructors declared as explicit are generally more popular than their non-explicit brothers. Because they prohibit the compiler from running non-expected (and often not expected) type conversions.

Unless I have a good reason to agree that the constructor is being used for implicit type conversions, I will declare it as explicit, encouraging you to follow the same policy.

Kezunhai Source: Http://blog.csdn.net/kezunhai Welcome to reprint or share. But be sure to declare the source of the article.

In-depth understanding of Explicitkeyword in C + +

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.