雷火电竞-中国电竞赛事及体育赛事平台

歡迎來(lái)到入門教程網(wǎng)!

C語(yǔ)言

當(dāng)前位置:主頁(yè) > 軟件編程 > C語(yǔ)言 >

c++隱式類型轉(zhuǎn)換示例分享

來(lái)源:本站原創(chuàng)|時(shí)間:2020-01-10|欄目:C語(yǔ)言|點(diǎn)擊:

復(fù)制代碼 代碼如下:

/*=============================================================================
#     FileName: explicit_try.cc
#         Desc: 驗(yàn)證含有一個(gè)參數(shù)的非explicit構(gòu)造函數(shù)是否可以拷貝初始化
=============================================================================*/
#include <iostream>
#include <string>
#include <vector>

using std::cout;
using std::cin;
using std::endl;
using std::string;
using std::vector;

class People {
    public:
        People() = default;
        People(string s):name(s) { }
        string getName() const { return name; }
        static vector<string> &getVector() { return name_arr; }
        //隱式類型轉(zhuǎn)換,用string生成一個(gè)臨時(shí)量,因此可以綁定到const形參上
        static void addToVector(const People &p) {
            name_arr.push_back(p.getName());
        }
    private:
        string name = "";
        static vector<string> name_arr;
};

vector<string> People::name_arr = {};

int main(int argc, const char *argv[])
{
    People p;
    cout << "p :" << endl;
    cout << p.getName() << endl;
    People tom("tom");
    People::addToVector(tom);
    string Bob = "Bob";
    People::addToVector(Bob);//隱式類型轉(zhuǎn)換
    //People::addToVector("Bob");//只允許一步的隱式類型轉(zhuǎn)換

    vector<string> v = People::getVector();
    cout << "name_arr:" << endl;
    for (const auto &p : v) {
        cout << p << " ";
    }
    cout << endl;

    string myName = "guo";
    People guo = myName; //隱式類型轉(zhuǎn)換允許拷貝初始化形式的轉(zhuǎn)換
    cout << guo.getName() << endl;
    return 0;
}



下面再來(lái)一個(gè)例子

復(fù)制代碼 代碼如下:

#include <string>
#include <iostream>
using namespace std;
class Fruit               //定義一個(gè)類,名字叫Fruit
{
 string name;     //定義一個(gè)name成員          
 string colour;   //定義一個(gè)colour成員

public:
 bool isSame(const Fruit &otherFruit)   //期待的形參是另一個(gè)Fruit類對(duì)象,測(cè)試是否同名
 {
  return name == otherFruit.name;
 }
 void print()              //定義一個(gè)輸出名字的成員print()
 {
  cout<<colour<<" "<<name<<endl;
 }
 Fruit(const string &nst,const string &cst = "green"):name(nst),colour(cst){}  //構(gòu)造函數(shù)

 Fruit(){}
};

int main()
{
 Fruit apple("apple");
 Fruit orange("orange");
 cout<<"apple = orange ?: "<<apple.isSame(orange)<<endl;  //沒(méi)有問(wèn)題,肯定不同
 cout<<"apple = /"apple/" ?:"<<apple.isSame(string("apple")); //用一個(gè)string做形參?

    return 0;
}

你會(huì)發(fā)現(xiàn)最后的使用上,我們用一個(gè)string類型作一個(gè)期待Fruit類形參的函數(shù)的參數(shù),結(jié)果竟然得出了是true(1),不要感到奇怪,這就是我現(xiàn)在要講的東西,隱式類類型轉(zhuǎn)換:“可以用單個(gè)實(shí)參來(lái)調(diào)用的構(gòu)造函數(shù)定義了從形參類型到該類型的一個(gè)隱式轉(zhuǎn)換?!保–++ Primer)首先要單個(gè)實(shí)參,你可以把構(gòu)造函數(shù)colour的默認(rèn)實(shí)參去掉,也就是定義一個(gè)對(duì)象必須要兩個(gè)參數(shù)的時(shí)候,文件編譯不能通過(guò)。然后滿足這個(gè)條件后,系統(tǒng)就知道怎么轉(zhuǎn)換了,不過(guò)這里比較嚴(yán)格:)以前我們構(gòu)造對(duì)象的時(shí)候Fruit apple("apple")其實(shí)也已經(jīng)有了一個(gè)轉(zhuǎn)換,從const char *的C字符串格式,轉(zhuǎn)為string,在這里,你再apple.isSame("apple")的話,蠢系統(tǒng)不懂得幫你轉(zhuǎn)換兩次,所以你必須要用string()來(lái)先強(qiáng)制轉(zhuǎn)換,然后系統(tǒng)才知道幫你從string隱式轉(zhuǎn)換為Fruit,當(dāng)然其實(shí)你自己也可以幫他完成。cout<<"apple = /"apple/" ?:"<<apple.isSame(Fruit("apple"));這樣。參考例子1.2 :Fruit apple = Fruit("apple");  //定義一個(gè)Fruit類對(duì)象apple。也就是這樣轉(zhuǎn)換的。不過(guò)這就叫顯式轉(zhuǎn)換了,我們不標(biāo)出來(lái),系統(tǒng)幫我們完成的,叫隱式的貝。這里要說(shuō)的是,假如你顯示轉(zhuǎn)換就可以不管有多少參數(shù)了,比如在前面提到的必須需要兩個(gè)參數(shù)的構(gòu)造函數(shù)時(shí)的例子。

例:

復(fù)制代碼 代碼如下:

#include <string>
#include <iostream>
using namespace std;
class Fruit               //定義一個(gè)類,名字叫Fruit
{
 string name;     //定義一個(gè)name成員          
 string colour;   //定義一個(gè)colour成員

public:
 bool isSame(const Fruit &otherFruit)   //期待的形參是另一個(gè)Fruit類對(duì)象,測(cè)試是否同名
 {
  return name == otherFruit.name;
 }
 void print()              //定義一個(gè)輸出名字的成員print()
 {
  cout<<colour<<" "<<name<<endl;
 }
 Fruit(const string &nst,const string &cst):name(nst),colour(cst){}  //構(gòu)造函數(shù)

 Fruit(){}
};

int main()
{
 Fruit apple("apple","green");
 Fruit orange("orange","yellow");
 cout<<"apple = orange ?: "<<apple.isSame(orange)<<endl;  //沒(méi)有問(wèn)題,肯定不同
 cout<<"apple = /"apple/" ?:"<<apple.isSame(Fruit("apple","green")); //顯式轉(zhuǎn)換
    return 0;
}


在你不想隱式轉(zhuǎn)換,以防用戶誤操作怎么辦?C++提供了一種抑制構(gòu)造函數(shù)隱式轉(zhuǎn)換的辦法,就是在構(gòu)造函數(shù)前面加explicit關(guān)鍵字,你試試就知道,那時(shí)你再希望隱式轉(zhuǎn)換就會(huì)導(dǎo)致編譯失敗,但是,要說(shuō)明的是,顯式轉(zhuǎn)換還是可以進(jìn)行。

上一篇:win32 api實(shí)現(xiàn)簡(jiǎn)單的消息窗口示例

欄    目:C語(yǔ)言

下一篇:c++判斷是否為目錄的示例分享

本文標(biāo)題:c++隱式類型轉(zhuǎn)換示例分享

本文地址:http://www.jygsgssxh.com/a1/Cyuyan/3727.html

網(wǎng)頁(yè)制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語(yǔ)言數(shù)據(jù)庫(kù)服務(wù)器

如果侵犯了您的權(quán)利,請(qǐng)與我們聯(lián)系,我們將在24小時(shí)內(nèi)進(jìn)行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負(fù)任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有