C + + type conversion functions

Source: Internet
Author: User

2202.cpp: Defines the entry point of the console application. #include "stdafx.h" #include <iostream>using namespace Std;class test{public:/* The conversion constructor allows you to convert the data of a specified type into an object of a class with a type conversion function that converts an object of a class to a data type of a specified type the general form of a conversion function is: operator type name () {The statement that implements the conversion} cannot specify a function type before the function name. function has no arguments The following example function name is: operator Int*/operator int () {return m_inum;} Private:int M_inum;}; int _tmain (int argc, _tchar* argv[]) {test test;int m = test;//This can be compiled through, if there is no type conversion function, there is a compile error here, because the class type cannot assign a value to int, once we define the//Type conversion function , it implicitly calls the int I;cin>>i;return 0;}

Conversion constructors allow you to convert data of a specified type to an object of a class

Use a type conversion function to convert an object of a class to a specified type of data

The general form of a type conversion function is:

operator type name ()

{The statement implementing the transformation}

Function type cannot be specified before function name, function has no arguments

C + + type conversion functions

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.