转换构造函数与类型转换函数

目录

1、转换构造函数

2、类型转换函数

3、小结


1、转换构造函数

        标准数据类型之间会进行隐式的类型安全转换 

        转换规则如下:

        

实例分析

#include <iostream>  
  
using namespace std;  
  
int main()  
{     
    short s = 'a';  
    unsigned int ui = 1000;  
    int i = -2000;  
    double d = i;  
      
    cout << "d = " << d << endl;  
    cout << "ui = " << ui << endl;  
    cout << "ui + i = " << ui + i << endl;   //  > 0    当无符号数和有符号数运算时,有符号数会被转化为无符号数
      
    if( (ui + i) > 0 )  
    {  
        cout << "Positive" << endl;  
    }  
    else  
    {  
        cout << "Negative" << endl;  
    }  
      
    cout << "sizeof(s + 'b') = " << sizeof(s + 'b') << endl;  
      
    return 0;  
}  

 

                                    已经在C语言进阶分析过,这里不再赘述

问题

            普通类型类类型之间能否进行类型转换? 

                    类类型之间能否进行类型转换? 

编程实验

普通类型 --> 类类型 test.cpp 

#include<iostream>  
using namespace std;  
  
class Test  
{  
public:  
    Test(){}  
};  
int main()  
{  
    Test t = 5;//毫无疑问编译不通过  
      
    return 0;  
 }  
#include<iostream>  
using namespace std;

class Test  
{
public:
    Test() {}
    Test(int i) {}
};
int main()
{
    Test t = 5;//却能编译不通过  

    return 0;
}

另一个视角

旧式的C方式强制类型转换 :

int i;   
Test t;   
  
i = int(1.5);   
t = Test(100);//编译通过,两者如此的相似  

编译器的行为:编译器会尽力尝试让编码通过编译 ,如下:

Test t;   
  
t = 100;  

转换构造函数:构造函数只有一个参数,该参数不是本类的const引用时,如:Test(int i)

解析:立即数100默认为int类型,怎么可能赋值给t对象呢!现在就报错吗?不急,编译器看看

           有没有转换构造函数! Ok, 发现Test类中定义了Test(int i) , 可以进行转换,默认等价于: t = Test(100); 

           编译器尽力尝试的结果是隐式类型转换 

           但隐式类型转换会让程序以意想不到的方式进行工作 ,是工程中bug的重要来源 

编译器如此厉害似乎应该很高兴,然而...

编程实验

隐式转换的危害 test.cpp

#include <iostream>  
#include <string>  
  
using namespace std;  
  
class Test  
{  
    int mValue;  
public:  
    Test()  
    {  
        mValue = 0;  
    }  
      
    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 = 5;   // t = Test(5);  
      
    Test r;  
      
    r = t + 10;   // r = t + Test(10);  可能是手误写错,编译却通过
      
    cout << r.value() << endl;  
      
    return 0;  
}  

   

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

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

          -三种显式转换方式

static_cast<ClassName>(value); 

ClassName(value); // 不推荐 
(ClassName)value; // 不推荐 

例如:

explicit Test(int i)  
{  
    mValue = i;  
}  

 此时再直接编译,报错,编译器不会进行转换尝试

main.cpp

int main()  
{     
    Test t;  

    t = static_cast<Test>(5);    // t = Test(5);  
    //t = (Test)5;   // t = Test(5);  

    Test r;  
      
    r = t + static_cast<Test>(10);   // r = t + Test(10);  
    //r = t + (Test)10;   // r = t + Test(10);  
      
    cout << r.value() << endl;  
      
    return 0;  
}  

 

2、类型转换函数

问题:类类型是否能够转换到普通类型

C++类中可以定义类型转换函数 

 语法规则:

operator Type ()  
{  
    Type ret;  
      
    //...  
      
    return ret;  
}  

编程实验

 类型转换函数初探  test.cpp

#include <iostream>  
#include <string>  
  
using namespace std;  
  
class Test  
{  
    int mValue;  
public:  
    Test(int i = 0)  
    {  
        mValue = i;  
    }  
    int value()  
    {  
        return mValue;  
    }  
    operator int ()  
    {  
        return mValue;  
    }  
};  
  
int main()  
{     
    Test t(100);  
    int i = t;  //t.operator int()
      
    cout << "t.value() = " << t.value() << endl;  
    cout << "i = " << i << endl;  
      
    return 0;  
} 

 

类型转换函数 

            -与转换构造函数具有同等的地位 

            -使得编译器有能力将对象转化为其它类型 

            -编译器能够隐式的使用类型转换函数 

编译器会尽力尝试让源码通过编译 ,如下

Test t(1);   
  
int i= t;   

解析:t这个对象为Test类型,怎么可能用于初始化int类型的变量呢!现在就报错吗?

           不急,看看有没有类型转换函数! Ok, 发现Test类中定义了operator int () , 可以进行转换。

编程实验

类类型之间的转换    test.cpp

#include <iostream>  

using namespace std;  
  
class Test;  
  
class Value  
{  
public:  
    Value()  
    {  
    }  
    explicit Value(Test& t) //若不加explicit,则会报错,两个转换函数都会隐式调用不知调用哪个,所以指明该转换构造函数必须显示调用
    {  
    }  
};  
  
class Test  
{  
    int mValue;  
public:  
    Test(int i = 0)  
    {  
        mValue = i;  
    }  
    int value()  
    {  
        return mValue;  
    }  
    operator Value()  
    {  
        Value ret;  
        cout << "operator Value()" << endl;  
        return ret;  
    }  
};  
  
int main()  
{     
    Test t(100);  
    Value v = t;  
      
    return 0;  
}  

  

无法抑制隐式的类型转换函数调用 

类型转换函数可能与转换构造函数冲突 

工程中以Type to Type()的公有成员代替类型转换函数 

#include <iostream>  
  
using namespace std;  
  
class Test;  
  
class Value  
{  
public:  
    Value()  
    {  
    }  
    Value(Test& t)  
    {  
    }  
};  
  
class Test  
{  
    int mValue;  
public:  
    Test(int i = 0)  
    {  
        mValue = i;  
    }  
    int value()  
    {  
        return mValue;  
    }  
    Value toValue()  
    {  
        Value ret;  
          
        return ret;  
    }  
};  
  
int main()  
{     
    Test t(100);  
    Value v = t.toValue();  
      
    return 0;  
} 

QT中的例子:qt_test.cpp

#include <QDebug>  
#include <QString>  
  
int main()  
{  
    QString str = "";  
    int i = 0;  
    double d = 0;  
    short s = 0;  
  
    str = "-255";  
    i = str.toInt();  
    d = str.toDouble();  
    s = str.toShort();  
  
    qDebug() << "i = " << i << endl;  
    qDebug() << "d = " << d << endl;  
    qDebug() << "s = " << s << endl;  
  
    return 0;  
}  

3、小结

转换构造函数只有—个参数 

转换构造函数的参数类型是其它类型 

转换构造函数在类型转换时被调用 

隐式类型转换是工程中bug的重要来源

explicit关键字用于杜绝隐式类型转换

C++类中可以定义类型转换函数 

类型转换函数用于将类对象转换为其它类型 

类型转换函数与转换构造函数具有同等的地位 

工程中以Type to Type()的公有成员代替类型转换函数 

 

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值