What is the role of explicit in C ++?

Source: Internet
Author: User

 

Explicit is used to prevent implicit conversions defined by constructors.

To understand its role, we must first understand implicit conversions: constructors that can be called with a single real parameter define an implicit conversion from the form parameter type to the class type.

For example:

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;};

  

Here, the things constructor can complete initialization with only one real parameter. Therefore, you can perform an implicit conversion, as shown below:

Things a; .................. // is initialized and used here. STD: String Nm = "book_1"; // since it can be implicitly converted, int result = A. compareto (Nm) can be used below );

This program uses a string type object as the compareto function passed to things as the real parameter. This function originally requires a struct object as a real parameter. Now the compiler uses string nm to construct and initialize a things object. The newly generated temporary things object is passed to the compareto function and is parsed after leaving this function.

The correctness of such behavior depends on the business needs. If you just want to test the weight-to-10 ratio of A, it may be convenient to do so. However, if the compareto function still involves the height attribute whose value is divided by the value initialized to 0, this operation may be incorrect. You need to change the height attribute not to 0 after constructing the struct. Therefore, this implicit type conversion should be restricted.

In this case, the constructor can be declared as explicit to prevent implicit type conversion.

The explicit keyword can only be used for the constructor declaration within the class, but not for the function definition outside the class. Now the things class is like this:

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;};

After compilation, the following message is displayed on vs2008: No user-defined conversion operator can be used to execute the conversion, or the operator cannot be called.

At this time, you can still use the constructor to complete the above type conversion:

Things a; .................. // is initialized and used here. STD: String Nm = "book_1"; // use the constructor int result = A. compareto (things (Nm ));

The advantage of explicit it mentioned in Google's c ++ specification is that it can avoid out-of-date type changes without any disadvantages. Therefore, Google stipulates that all single-parameter constructors must be displayed. In rare cases, the copy constructor can not be declared as explicit. For example, a class that acts as a transparent package for other classes.

The constructor declared as explicit is generally more popular than its non-explicit brother, tive C ++ said. They prevent the compiler from executing unexpected (and often not expected) type conversions. Unless I have a good reason to allow the constructor to be used for implicit type conversion, I will declare it as explicit. I encourage you to follow the same policy.

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.