C + + Implicit class-type conversions

Source: Internet
Author: User

C + + can define how to implicitly convert objects of other types to our class types or implicitly convert objects of our class type to other types. In order to define an implicit conversion to a class type, you need to define the appropriate constructor.

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

Let's take a look at the following example: http://blog.csdn.net/vagrxie/article/details/1586340

1#include <string>2#include <iostream>3 using namespacestd;4 5 classFruit//define a class named fruit.6 {7     stringName//define a name member8     stringColour;//define a colour member9 Ten  Public: One     BOOLIssame (ConstFruit &otherfruit)//the expected formal parameter is another fruit class object that tests for the same name A     { -         returnName = =Otherfruit.name; -     } the     voidPrint ()//define a member of the Output name print () -     { -cout<<colour<<" "<<name<<Endl; -     } +Fruit (Const string&nst,Const string&AMP;CST ="Green"): Name (NST), colour (CST) {}//constructor Function -  + Fruit () {} A }; at  - intMainvoid) - { -Fruit Apple ("Apple"); -Fruit Orange ("Orange"); -cout<<"Apple = orange?:"<<apple.issame (orange) <<endl;//no problem, definitely different. incout<<"Apple = Apple?:"<<apple.issame (string("Apple")) <<endl;//use a string to make a formal parameter?  -  to     return 0; +}

We use the string ("Apple") type as an argument for a function that expects the fruit type parameter, and the result is correct. When you remove the default argument for the constructor colour, which is to define an object that must have two arguments, the file compilation does not pass. Fruit Apple ("Apple") has actually already had a conversion from the const char * C string format to string. But Apple.issame ("Apple") is definitely wrong, and must be cast with string () before the system knows to help you implicitly convert from string to fruit. Of course you can also do it explicitly, Apple.issame (Fruit ("Apple")).

You can prevent constructors from being used in contexts that require implicit conversions by declaring the constructor as explicit. The explicit keyword can only be used on a constructor declaration inside a class. A definition made outside the definition body of a class no longer repeats it. Although it cannot be implicitly converted, an explicit conversion is still possible, for example: Apple.issame (Fruit ("Apple")).

Description

(1) explicitly using a constructor simply terminates the use of the constructor implicitly. Any constructor can be used to explicitly create a temporary object.

(2) Normally, a single parameter constructor should be explicit unless there is a clear reason to define an implicit conversion. Setting the constructor to explicit avoids errors, and when the conversion is useful, the user can explicitly construct the object.

(3) The benefit of setting the constructor to explicit is to avoid semantic errors due to implicit type conversions, and the disadvantage is that when the user does need to do the appropriate type conversion, the implicit type conversion cannot be relied upon, and the temporary object must be explicitly created.

C + + Implicit class-type conversions

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.