Explicit keywords in C + +

Source: Internet
Author: User

Read the knowledge about initialization in effective C + +, which mentions the explicit keyword

Explicit function: Prevents the constructor from performing an implicit type conversion

To understand its role, first understand the implicit conversion:

Let's construct an implicit call.

Method: A constructor that can be called with a single argument defines an implicit conversion from the formal parameter type to the class type.

For example:

<span style= "FONT-SIZE:18PX;" >class b{public   :           B (int x);       void DoSomething (B bobject);} </span>

Then there is an action call dosomething

As follows:

<span style= "FONT-SIZE:18PX;" >dosomething (;</span>)
The dosomething parameter type is B, but the 20 used here can actually be executed.

Because the 20 here calls the constructor B (20) to become a B object

Again as follows:

<span style= "FONT-SIZE:18PX;" >class things{public    :        things (const STD::STRING&NAME = ""):              m_name (name), height (0), weight (10) {}        int CompareTo (const things & other);        std::string m_name;        int height;        int weight;}; </span>

Here the things constructor can be initialized with just one argument. So you can do an implicit conversion, like this:

things A;
................//is initialized and used here.
std::stringnm="Book_1";
//because you can implicitly convert, you can use the following
intresult=A.compareto (nm);

This program uses a string type object as an argument to pass the CompareTo function to things. This function would have required a Tings object as an argument. Now the compiler uses string nm to construct and initialize a

The things object, the newly generated temporary things object, is passed to the CompareTo function and is refactored after leaving the function.

The correctness of this behavior depends on the business needs. If you just want to test the ratio of the weight of a to the size of 10, it might be convenient to do so. If, however, the CompareTo function involves dividing the height property to be initialized to 0, it might be wrong to do so. You need to change the height property to not 0 after you construct tings. So to limit this implicit type conversion.

It is then possible to prevent implicit type conversions by declaring the constructor as explicit.

The explicit keyword can only be used on a constructor declaration inside a class, not on a function definition outside the class. Now the things class looks like this:

<span style= "FONT-SIZE:18PX;" >class things{public    :        explicit Things (const STD::STRING&NAME = ""):              m_name (name), height (0), Weight (0) {}        int CompareTo (const things & other);        std::string m_name;        int height;        int weight;}; </span>

when you compile, you will be prompted on vs2008: There is no user-defined conversion operator available to perform the transformation, or the operator cannot be called.

You can still use the constructor to complete the above type conversion by showing:

Things A;
................//is initialized and used here.
std::stringnm="Book_1";
//display using constructors
intresult=A.compareto (Things (nm));

The advantage of Google's C + + specification for explicit is that it avoids outdated types of transformations, which have no drawbacks. So Google agreed that all single-parameter constructors must be displayed, and in rare cases the copy constructor can not declare 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 performing type conversions that are not expected (and are often not expected). Unless I have a good reason to allow the constructor to be used for implicit type conversions, I will declare it as explicit. I encourage you to follow the same policy.


Explicit keywords 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.