41. Type conversion function

Implicit type-safe conversions are performed between standard data types. The rules are as follows:

char->short->int->unsigned int->long->unsigned long->float->double   小转大

#include <iostream>

#include <string>
using namespace std;
int main()
{   
    short s = 'a'; // char to short
    unsigned int ui = 1000; // int to unsigned int
    int i = -2000;                     
    double d = i ; // int to double
    cout << "d = " << d << endl;
    cout << "ui = " << ui << endl;       
    cout << "ui + i = " << ui + i < < endl;
    if( (ui + i) > 0 ) //Signed and unsigned addition, convert i to unsigned int by default, which is equivalent to adding two unsigned
    {
        cout << "Positive" << endl ;
    }
    else
    {
        cout << "Negative" << endl;
    }    
    cout << "sizeof(s + 'b') = " << sizeof(s + 'b') << endl; // convert both s and 'b' to int, print 4
    return 0;

}

Can type conversion be performed between ordinary types and class types? Can the class types be converted to each other?

Let's talk about the constructor again,

Constructors can define parameters of different types.

A conversion constructor is called when the parameters meet the following conditions:

There is one and only one parameter, the parameter is a primitive type, or the parameter is another class type.


 Old-style c-way cast: int i; Test t; i = int(5); t = Test(100);

The compiler will try its best to make the source code compile, Teat t; t=100; 100 is an immediate value of int type by default, how can it be assigned to the t object? Test(int i) can be converted, the default is equivalent to: t=Test(100);

Test t; t=5; // t=Test(5); implicit type conversion

The result of the compiler's best attempt is an implicit type conversion

Implicit type conversions: Make programs work in unexpected ways and are an important source of engineering bugs.

In the project, the explicit keyword is used to eliminate the compiler's conversion attempt. When the conversion constructor is modified by explicit, only display conversion can be performed.

Coercion method: static_cast<ClassName>(value); //in c++

                ClassName(value); //in c language

                (ClassName)value; //In c language, not recommended

#include <iostream>
#include <string>
using namespace std;
class Test
{
    int mValue;
public:
    Test()
    {
        mValue = 0;
    }    
    explicit Test(int i)           // 杜绝隐式转换
    {
        mValue = i;
    }   
    Test operator + (const Test& p)
    {
        Test ret(mValue + p.mValue);        
        return ret;
    }    
    int value()
    {
        return mValue;
    }
};
int main()
{   
    Test t;    
    t = static_cast<Test>(5);             // t = Test(5);    
    Test r ;    
    r = t + static_cast<Test>(10);   // r=t+10;   <---->    r = t + Test(10);   
    cout << r.value() << endl;    
    return 0;

}

static_cast: conversion between primitive types and class types

The conversion constructor has only one parameter, and its parameter type is other type, which is called during type conversion. It is an important source of bugs during implicit conversion. Explcit is used to eliminate implicit type conversion.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325953569&siteId=291194637