Recognize the C ++ language-also talk about the C ++ keyword explicit

Source: Internet
Author: User

The explicit keyword in C ++ is used and can only be used to modify the class constructor, indicating that the constructor is explicit, "explicit" means that the name of the constructor must be written when the class object is created. "implicit" means that the constructor name is not required when the class object is created, you only need to specify the real parameter based on the constructors parameter type. The Compiler performs implicit conversion from the form parameter type to the class type.

C ++ primer states that a constructor that can be called with a single real parameter defines an implicit conversion from the form parameter type to the class type.

That is to say, if the constructor of the C ++ class can pass only one real parameter for calling (the constructor may actually have more than one parameter, but apart from passing in the parameter of the real parameter, other parameters have default values), so there will be a default conversion operation during compilation: Convert the data of the corresponding data type of the constructor to this type of object, as shown below:
Class aceclass
{
Public:
Aceclass (INT age );

Aceclass (const STD: string & name );

Aceclass (STD: istream & IS );
}
....

Aceclass OBJ (20); // OK, explicitly calling the constructor
Aceclass obj2 = 20; // OK, implicit conversion, convert int to aceclass

String nullname = "null ";

Aceclass acename = nullname; // OK, implicit conversion, String Conversion to aceclass

Aceclass acestream = CIN; // OK, implicit conversion, converting STD: istream to aceclass
In the code above, the compiler automatically converts an integer to an aceclass class object, which is actually equivalent to the following operation:

// The compiler uses an aceclass constructor that accepts int to generate a new aceclass object temp from 20.
Aceclass temp (20 );

// The new temporary object temp is passed to obj2
Aceclass obj2 = temp;
All the operations above are called "implicit conversions ".

Sometimes this implicit conversion will cause unnecessary confusion to the code reader. Therefore, we need to avoid the compiler's automatic conversion function in many cases. In this case, the keyword "Explicit" mentioned at the beginning of the article is used. Declare the constructor of the class 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 aceclass class to be explicit, the following code cannot be compiled, as shown below:
Class aceclass
{
Public:
Explicit aceclass (INT num );

Explicit aceclass (const STD: string & name );

Explicit aceclass (STD: istream & IS );

...
}
....

Aceclass OBJ (20); // OK, explicitly calling the constructor
Aceclass OBJ 2 = 20; // err, cannot be implicitly converted

String nullname = "null ";

Aceclass acename = nullname; // err, which cannot be implicitly converted

Aceclass acestream = CIN; // err, which cannot be implicitly converted

 

Note: The explicit keyword can only be used for the constructor declaration inside the class. You should not repeat it on the definition made outside the class definition body:

// Err. Explicit allowed only on constructor declaration in Class header

Explicit aceclass: aceclass (INT num)

{

...

}

 

Here, we will add one point: the explicit keyword does not work on constructors with more than one parameter, because when there are more than one constructor parameter, it does not perform implicit type conversion by default. In other words, the constructor is the same without explicit it. Of course, as mentioned above, one exception is that when only one real parameter is passed when the constructor is called, and other parameters have default values, at this time, the explicit keyword still plays its due role.

The description of explicit in msdn is as follows:ExplicitOn a constructor with multiple arguments has no effect, since such constructors cannot take part in implicit conversions. however, for the purpose of implicit conversion, explicit will have an effect if a constructor has multiple arguments and all but one of the arguments has a default value.

 

Best Practice: unless there are obvious reasons to define implicit conversions, the single-parameter constructor should be declared as explicit. Setting the constructor to explicit can avoid errors. When the conversion is available, you can display the constructor objects.

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.