C++ 类类型转换函数explicit 关键字

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

  转换规则:

  

 隐式类型转换的问题:  

#include <iostream>
#include <string>

using namespace std;

int main()
{
    short s = 'a';
    unsigned int ui = 1000;
    int i = -2000;
    double d = i;

    cout <<"d = "<<d<<endl;//输出d = -2000
    cout <<"ui= "<<ui<<endl;//输出 ui = 1000
    cout <<"ui+i = "<<ui+i<<endl;//等价于 unsigned int ui +(unsigned int)i = ?

    
    
}
 cout <<"ui+i = "<<ui+i<<endl;//等价于 unsigned int ui +(unsigned int)i = ?

根据:-2000 + 1000 应该等于-1000,但是这里编译器帮我们做了隐式类型转换将  int 类型的变量 i 隐式类型转换为 unsigned int i 然后进行 i 与 ui 相加。所以出现了问题。

普通类型与类类型进行强制类型转换。  

  使用转换构造函数。

#include <iostream>
#include <string>

using namespace std;


/*
使用普通类型强制转换为 类类型。
转换构造函数:
    -有仅有一个参数
    -参数是基本类型
    -参数是其它类型
    使用explicit 关键字。
*/
class Test
{
    int mvalue;
    public:
        int getValue()
            {
                return mvalue;
            }
        Test ()
            {
                mvalue = 0;
            }
     explicit Test(int i)//转换构造函数
            {
                mvalue = i;
            }
};

int main()
{    
    Test t;

    cout << "t.getValue() = " <<t.getValue() <<endl;//输出t.getValue() = 0
//    t = 5;//这里等价于   t= Test(5); 
//    Test tt= Test(5);
    t = static_cast<Test>(5);
    cout << "t.getValue() = " <<t.getValue() <<endl;//输出t.getValue() = 5
    t = Test(10);
    cout << "t.getValue() = " <<t.getValue() <<endl;//输出t.getValue() = 10
    return 0;
}

运行结果:

t.getValue() = 0
t.getValue() = 5

t.getValue() = 10

 

  在实际工程中使用explicit 关键字杜绝编译器的转换尝试。

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

转换方式:

  static_cast<className>(value);

  className(value);例如:t= Test(5); 

 

将类类型转换为普通类型:

  C++ 中可以定义类型转换函数,用作将类转换为其它类型。

  语法规则:

    operator Type()

      {

        Type ret ;

        ........

        return ret;

       }

#include <iostream>
#include <string>

using namespace std;


/*
将类类型转换为普通类型:

*/
class Test
{
    int mvalue;
    public:
        int getValue()
            {
                return mvalue;
            }
        Test ()
            {
                mvalue = 0;
            }
       Test(int i)
            {
                mvalue = i;
            }
       operator int()
           {
               return mvalue;
           }
};

int main()
{    
    Test t(100);
    int i = t;
    cout << "i = "<< i<<endl;//输出i = 100
    return 0;
}

  

类类型互相转换:

  定义类型转换函数

#include <iostream>
#include <string>

using namespace std;


/*
类类型互相转换

*/

class Test;
class value
{
    int mvalue;
    public:
        int getValue()
            {
                return  mvalue;
            }
        value(int i)
            {

                mvalue= i;
            }
        explicit value(Test & t)
            {
                
            }
};
class Test
{
    int mvalue;
    public:
        int getValue()
            {
                return mvalue;
            }
        Test ()
            {
                mvalue = 0;
            }
       Test(int i)
            {
                mvalue = i;
            }
       operator int()//类型转换函数
           {
               return mvalue;
           }
       value tovalue ()//类型转换函数      ,工程中以Type toType() 的公有成员函数代替。
           {
//               value ret(this->getValue()); 
        value ret(mvalue); 

            cout << "operator value ()"<<endl;
            return ret;
           }
};

int main()
{    
    Test t(100);
    int i = t;
    cout << "i = "<< i<<endl;

    value tt  = t.tovalue();
    cout << tt.getValue()<<endl;
    return 0;
}

类型转换函数:

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

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

  -工程中以Type toType() 的公有成员函数代替。

猜你喜欢

转载自www.cnblogs.com/hjxzjp/p/11768674.html