zoukankan      html  css  js  c++  java
  • C++中的类型转换函数

    1,转换构造函数可以将普通的基础类型转换为当前的类类型,也有能力将其它类类 型的对象转换为当前的类类型;

     

    2,问题:

        1,类类型是否能够类型转换到普通类型?

           1,可以的;

       

    3,类型转换函数:

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

        2,类型转换函数用于将类对象转换为其他类型;

           1,不管什么类型都可以;

        3,语法规则:

            1,代码示例:

    1 operator Type ()  // operator 表示定义类型转换函数,Type 表示返回值,没有参数;
    2 {    // 函数体表示普通的函数体内容;
    3     Type ret;
    4                
    5     // ...
    6                
    7     return ret;
    8 }

        4,类型转换函数初探编程实验:

           1,main.cpp 文件:

     1 #include <iostream>
     2 #include <string>
     3 
     4 using namespace std;
     5 
     6 class Test
     7 {
     8     int mValue;
     9 public:
    10     Test(int i = 0)
    11     {
    12         mValue = i;
    13     }
    14     int value()
    15     {
    16         return mValue;
    17     }
    18     operator int ()  // 类型转换函数;
    19     {
    20         return mValue;
    21     }
    22 };
    23 
    24 int main()
    25 {   
    26     Test t(100);
    27     int i = t;  // ==> int i = t.operator int(); 隐式调用类型转换成员函数;这里实质是隐式类型转换;
    28     
    29     cout << "t.value() = " << t.value() << endl;
    30     cout << "i = " << i << endl;
    31     
    32     return 0;
    33 }

          

    4,类型转换函数:

        1,与转换构造函数具有同等的地位;

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

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

        4,编译器会尽力尝试让源码通过编译:

     

           1,这里实质是隐式类型转换;

     

    5,类类型之间的相互转换:

        1,类型转换函数 VS 转换构造函数

        2,类类型之间的转换编程实验:

             1,main.cpp 文件:

     1 #include <iostream>
     2 #include <string>
     3 
     4 using namespace std;
     5 
     6 class Test;
     7 
     8 class Value
     9 {
    10 public:
    11     Value()
    12     {
    13     }
    14     explicit Value(Test& t)
    15     {
    16     }
    17 };
    18 
    19 class Test
    20 {
    21     int mValue;
    22 public:
    23     Test(int i = 0)
    24     {
    25         mValue = i;
    26     }
    27     int value()
    28     {
    29         return mValue;
    30     }
    31     operator Value()
    32     {
    33         Value ret;
    34         cout << "operator Value()" << endl;
    35         return ret;
    36     }
    37     /*
    38     工程上通过以下方式;
    39     Value toValue()
    40     {
    41         Value ret;
    42         
    43         return ret;
    44     }
    45     */
    46 };
    47 
    48 int main()
    49 {   
    50     Test t(100);
    51     Value v = t;  // ==> t.operator value();
    52     // Value v = t.toValue();  // 工程上针对类型转换函数来杜绝隐式类型转换的用法;
    53     
    54     return 0;
    55 }

        2,编译输出:

             1,当 Value 类构造函数不加 explicit 修饰时:

               error: conversion from ‘Test’ to ‘Value’ is ambiguous

               note: candidates are: Test::operator Value()

               note:                 Value::Value(Test&)

             2,当 Value 类构造函数加 explicit 修饰时:

                 1,编译通过;

        3,转换构造函数和类型转换函数是互逆的,但是当遇到类类型之间的转换时,它们之间是有冲突的,编译器不知道如何选择,应该在转换构造函数之前加 explicit 修饰;

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

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

             1,可通过 explicit 修饰转换构造函数解决;

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

             1,工程中一般不定义类型转换函数;

             2,抑制类型转换函数调用;

             3,Qt 中的示例:

     1 #include <QDebug>
     2 #include <QString>
     3 
     4 int main()
     5 {
     6     QString str = "";
     7     int i = 0;
     8     double d = 0;
     9     short s = 0;
    10 
    11     str = "-255";
    12 
    13     /* 以下进行了类类型到基础类型之间的转换 */
    14     i = str.toInt();
    15     d = str.toDouble();
    16     s = str.toShort();
    17 
    18     qDebug() << "i = " << i << endl;
    19     qDebug() << "d = " << d << endl;
    20     qDebug() << "s = " << s << endl;
    21 
    22     return 0;
    23 }

     

    5,小结:

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

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

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

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

  • 相关阅读:
    正则表达式match方法和search方法
    正则表达式(基础篇1)
    动画
    重绘和重排(回流)
    数组常用的10个方法
    css3只需一招,将网站变成灰色的
    Python-类的几种调用方法
    Codeforces Global Round 8 C. Even Picture(构造)
    Codeforces Global Round 8 D. AND, OR and square sum(位运算)
    Codeforces Round #650 (Div. 3) C. Social Distance
  • 原文地址:https://www.cnblogs.com/dishengAndziyu/p/10914348.html
Copyright © 2011-2022 走看看