Overload, type conversion, and operators

Source: Internet
Author: User

We can see that a non-explicit constructor called by a real parameter defines an implicit type conversion. This constructor converts an object of the real parameter type to a class type. We can also define the type conversion for the class type, which can be done by defining the type conversion operator. The conversion constructor and the type conversion operator define class type conversion. Such conversion is also called user-defined type conversion.

Type conversion Operator

A type conversion operator is a special member function of a class. It is used to convert values of a class type to other types. The general form of the type conversion function is as follows:

Operateor type () const;

Type indicates a type. The type conversion operator can be defined for any type (except void), as long as the type can be used as the return type of the function. Therefore, we do not allow conversion to arrays or function types, but can convert to pointers (including array pointers and function pointers) or reference types.

The type conversion operator neither explicitly returns the type nor has the form parameter, and must be defined as a member function of the class. The content of the object to be converted should not be changed by the type conversion operator. Therefore, the type conversion operator is generally defined as a const member.

Note: A type conversion function must be a member function of the class. It cannot declare the return type, and the parameter list must be empty. The type conversion function is usually Const.

Define classes containing type conversion Operators

Overload, type conversion, and operators

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.