"C + +" implicit type conversion and explicit keywords

Source: Internet
Author: User

From: Huang Bongyong Handsome

1. When a constructor with one parameter is in the class, an implicit conversion of the holding object, such as a Class A, and a constructor with a parameter, a (int i) {}, is invoked when statement a m=1 is present, and a constructor with one argument is called to create the object m, that is, to convert the statement to a M (1).


2. If there is a function such as void F (A J) {} This function requires an object of Class A as a parameter, but when the call statement is F (2), the function can also be called, when the function's formal parameter A J is interpreted as a j=2 that implicitly invokes a constructor with one argument to create an object J.

However, there is a form of function definition when a call such as the statement F (2) error occurs, that is, the function f definition of void F (A &j) {} defines a reference to accept an argument error, the reason is unclear. In these cases, however, void F (A J) {},void F (const a J) {};void F (Consta & j) {} is correctly called.


3. If this implicit type conversion is not required, the explicit is used before the constructor, and the closing word can only be used before the constructor. If explicit is used before the constructor, statements A M=1 and F (2) will have an error.

4. A constructor declared as explicit cannot be used in an implicit conversion.

"C + +" implicit type conversion and explicit keywords

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.