zoukankan      html  css  js  c++  java
  • 第四十一课、类型转化构造函数(上)

    一、C语言中的类型转换


    1、标准数据类型之间会进行隐式的类型安全转换
    2、转换规则如下

    3、由此可知,如果是小类型转到大类型,C语言是支持隐式转换,是安全的

    #include<iostream>
    
    using namespace std;
    
    int main()
    {
        short s = 'a';//小类型转化为大类型,安全
        unsigned int ui = 1000;//1000默认为int,小类型转化为大类型
        int i = -2000;
        double d = i;
    
        cout << d << endl;
    
        if((ui + i) > 0)//unsigned int + int =====> unsigned int + unsigned int > 0
        {
            cout << "Positive" << endl;//输出这个
        }
        else
        {
            cout << "Negative" << endl;
        }
    
        cout << "sizeof(s + 'b')=" << sizeof(s + 'b') << endl;//4, short + char ====> int + int, 编译器认为int效率更快
        return 0;
    }

    二、再论构造函数

    1、构造函数可以定义不同类型的参数
    2、参数满足下列条件时称为转换构造函数
    (1)、有且仅有一个参数
    (2)、参数是基本类型
    (3)、参数是其它类类型

    3、旧式的c方式强制类型转换

    int i;
    Test t;
    
    i = int(1.5);
    t = Test(100);
    #include<iostream>
    
    using namespace std;
    
    class Test
    {
    private:
        int mValue;
    public:
        Test()
        {
            mValue = 0;
        } 
    
        Test(int i)
        {
            mValue = i;
        }
    
        int value()
        {
            return mValue;
        }
    
        Test operator + (const Test& p)
        {
            Test ret(mValue + p.mValue);
            return ret;
        }
    
    };
    
    int main()
    {
        Test t;
        t = 100;//编译器会尽量使源码编译通过
           //100 默认为int 类型,编译器会去寻找看有没有转换构造函数发现 Test(int)
           //所以这里相当于 t = Test(100)

    cout
    << t.value() << endl;//100 Test tt; tt = t + 10;//这里是本意这样做还是手误? bug 的重要来源 cout << tt.value() << endl;//110 return 0; }

    三、避免编译器的默认类型转换

    1、工程中通过explicit关键字杜绝编译器的转换尝试

    2、转换构造函数被explicit修饰时只能进行显示转换

    3、转换方式

    (1)、static_cast<ClassName>(value)//c++推荐的方式

    (2)、ClassName(value)

    (3)、(ClassName)value //不推荐

    #include<iostream>
    
    using namespace std;
    
    class Test
    {
    private:
        int mValue;
    public:
        Test()
        {
            mValue = 0;
        } 
    
        explicit Test(int i)//explicit 关键字
        {
            mValue = i;
        }
    
        int value()
        {
            return mValue;
        }
    
        Test operator + (const Test& p)
        {
            Test ret(mValue + p.mValue);
            return ret;
        }
    
    };
    
    int main()
    {
        Test t;
        t = static_cast<Test>(100);//没有强制类型转换时会报错
        cout << t.value() << endl;//100    
    
        Test tt;
        tt = t + static_cast<Test>(10);
        cout << tt.value() << endl;//110
    
        
        return 0;
    }

    四、小结

    1、转换构造函数只有一个参数
    2、转换构造函数的参数类型是其它类型
    3、转换构造函数在类型转换时被调用
    4、隐式类型转换是工程中bug的重要来源

    5、explicit关键字杜绝隐式类型转换

     

  • 相关阅读:
    关于struts2的checkboxlist、select等标签发生could not be resolved as a collection/array/map/enumeration/iterator type异常的记录
    ajax Session失效如何跳转到登录页面
    将默认首页设置成index.do的方法
    tomcat应用转到weblogic上时的问题
    MyEclipse 8.5整合Git,并在Github上发布项目(转)
    C++ 类设计核查表
    编程书籍
    并发模型
    C++ AOP手法
    编程 网上资源平台
  • 原文地址:https://www.cnblogs.com/gui-lin/p/6364621.html
Copyright © 2011-2022 走看看