Implicit type conversion of C ++

Source: Internet
Author: User

Implicit type conversion of C ++

C ++ is a complex language with many "fun" features. Learning C ++ is like picking up a rock on the beach, maybe you can build your own little castle with a small rock.

After talking nonsense, let's talk about the stone we found: implicit type conversion.

Source: Translation of Objective C ++ lostmouse

 


Class TestInt
{

Public:
Int GetData () const {return I ;};
TestInt (int ii): I (ii) {}; // Constructor

Private:
Int I;

};

Void fun (TestInt t)

{
Cout <t. GetData () <endl;
}

Int main ()

{

Fun (10 );

Return 0;

}

Running result:

10

Why does the fun function need parameters of the TestInt type? Can it be passed into the int type,

Before looking for the cause, comment out the constructor and re-compile the constructor. The result is that the following error occurs: conversion from 'int' to non-scalar type 'testint.

It seems a little eye-catching. The successful call estimation is related to the constructor of the class. In fact, this is the implicit type conversion in C ++:

The compiler knows that the value of fun is int and the function requires TestInt. However, the compiler also knows to call the TestInt constructor to convert int into a suitable TestInt,

We know that the function will generate a temporary variable when passing the value. The current situation is similar to const TestInt t (10), so the result is shown above.

 

Example in Objective C ++:

Class Month {
Public:
Static const Month Jan () {return 1 ;}
Static const Month Feb () {return 2 ;}
...
Static const Month Dec () {return 12 ;}

Int asInt () const // For convenience, make the Month
{Return monthNumber;} // can be converted to int

Private:
Month (int number): monthNumber (number ){}

Const int monthNumber;
};

At the beginning, I didn't understand how to call this class, and on static const Month Jan () {return 1 ;}

The return value of this function has a lot of questions. Why is the return type Month, but the function can return an int.

I couldn't figure it out, so I had to break into the compiler and try again and again, and finally figured out the usage of this class,

In fact, this class is to get the Month of a const: Month jan = Month: Jan (); in this way, we will get the object representing January.

Static const Month Jan () {return 1;} successfully utilizes implicit type conversion, but the current constructor is

Private to prevent users from creating new month.

 

"Reading a swimming book doesn't really make you learn to swim." The same is true for programming.

 

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.