日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達者為師

網(wǎng)站首頁 編程語言 正文

C++圖文并茂講解類型轉(zhuǎn)換函數(shù)_C 語言

作者:清風自在?流水潺潺 ? 更新時間: 2022-07-28 編程語言

一、類型轉(zhuǎn)換函數(shù)(上)

1.再論類型轉(zhuǎn)換

標準數(shù)據(jù)類型之間會進行隱式的類型安全轉(zhuǎn)換

轉(zhuǎn)換規(guī)則如下:

下面來看一個有趣的隱式類型轉(zhuǎn)換:

#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;
    cout << "ui = " << ui << endl;
    cout << "ui + i = " << ui + i << endl;
    if( (ui + i) > 0 )
    {
        cout << "Positive" << endl;
    }
    else
    {
        cout << "Negative" << endl;
    }
    cout << "sizeof(s + 'b') = " << sizeof(s + 'b') << endl;
    return 0;
}

輸出結(jié)果如下:

ui 為 unsigned int 類型,i 為 int 類型,將兩者進行相加編譯器會進行隱式的類型轉(zhuǎn)換,全部變成unsigned int 類型,所以最后的運行結(jié)果是正數(shù)。

s 和 'b 編譯器都被編譯器轉(zhuǎn)換成 int 類型,因為 int 類型的運算是最高效的。

2.問題

普通類型與類類型之間能否進行類型轉(zhuǎn)換?類類型之間能否進行類型轉(zhuǎn)換?

3.再論構(gòu)造函數(shù)

構(gòu)造函數(shù)可以定義不同類型的參數(shù)

參數(shù)滿足下列條件時稱為轉(zhuǎn)換構(gòu)造函數(shù)

  • 有且僅有一個參數(shù)
  • 參數(shù)是基本類型
  • 參數(shù)是其它類類型

4.另一個視角

舊式的 C 方式強制類型轉(zhuǎn)換

5.編譯器的行為

編譯器會盡力嘗試讓源碼通過編譯

編譯器盡力嘗試的結(jié)果是隱式類型轉(zhuǎn)換

隱式類型轉(zhuǎn)換

會讓程序以意想不到的方式進行工作

是工程中 bug 的重要來源

工程中通過 explicit 關(guān)鍵字杜絕編譯器的轉(zhuǎn)換嘗試

轉(zhuǎn)換構(gòu)造函數(shù)被 explicit 修飾時只能進行顯示轉(zhuǎn)換

轉(zhuǎn)換方式

下面來看一個普通類型向類類型的轉(zhuǎn)換:

#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 + Test(10);
    cout << r.value() << endl;
    return 0;
}

輸出結(jié)果如下:

6.小結(jié)(上)?

  • 轉(zhuǎn)換構(gòu)造函數(shù)只有一個參數(shù)
  • 轉(zhuǎn)換構(gòu)造函數(shù)的參數(shù)類型是其它類型
  • 轉(zhuǎn)換構(gòu)造函數(shù)在類型轉(zhuǎn)換時被調(diào)用
  • 隱式類型轉(zhuǎn)換是工程中 bug 的重要來源
  • explicit關(guān)鍵字用于杜絕隱式類型轉(zhuǎn)換

二、類型轉(zhuǎn)換函數(shù)(下)

1.類型轉(zhuǎn)換

類類型是否能夠類型轉(zhuǎn)換到普通類型?

C++ 類中可以定義類型轉(zhuǎn)換函數(shù)

類型轉(zhuǎn)換函數(shù)用于將類對象轉(zhuǎn)換為其它類型

語法規(guī)則:

下面來看一個類型轉(zhuǎn)換函數(shù):

#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;
    cout << "t.value() = " << t.value() << endl;
    cout << "i = " << i << endl;
    return 0;
}

int i = t; 等價于int i = t.operator int ();

類型轉(zhuǎn)換函數(shù)

  • 與轉(zhuǎn)換構(gòu)造函數(shù)具有同等的地位
  • 使得編譯器有能力將對象轉(zhuǎn)化為其它類型
  • 編譯器能夠隱式的使用類型轉(zhuǎn)換函數(shù)

2.編譯器的行為

編譯器會盡力嘗試讓源碼通過編譯

3.注意事項

  • 無法抑制隱式的類型轉(zhuǎn)換函數(shù)調(diào)用
  • 類型轉(zhuǎn)換函數(shù)可能與轉(zhuǎn)換構(gòu)造函數(shù)沖突
  • 工程中以 Type toType() 的公有成員代替類型轉(zhuǎn)換函數(shù)

下面看類類型之間的轉(zhuǎn)換:

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

輸出結(jié)果如下:

注意類型轉(zhuǎn)換函數(shù)可能與轉(zhuǎn)換構(gòu)造函數(shù)沖突,所以 explicit Value(Test& t) { } 加了一個 explicit,如果不加,編譯器可能會報錯。

4.小結(jié)(下)

  • C++ 類中可以定義類型轉(zhuǎn)換函數(shù)
  • 類型轉(zhuǎn)換函數(shù)用于將類對象轉(zhuǎn)換為其它類型
  • 類型轉(zhuǎn)換函數(shù)與轉(zhuǎn)換構(gòu)造函數(shù)具有同等的地位
  • 工程中以 Type toType() 的公有成員代替類型轉(zhuǎn)換函數(shù)

原文鏈接:https://blog.csdn.net/weixin_43129713/article/details/124551084

欄目分類
最近更新