• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            隨筆 - 8  文章 - 26  trackbacks - 0
            <2025年7月>
            293012345
            6789101112
            13141516171819
            20212223242526
            272829303112
            3456789

            常用鏈接

            留言簿(5)

            隨筆檔案

            文章分類

            文章檔案

            相冊

            C++語言

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

            一個explicit關鍵字聲明的構(gòu)造函數(shù)禁止編譯器進行隱式類型轉(zhuǎn)換 .(禁止單參數(shù)構(gòu)造函數(shù)"被用于自動型別轉(zhuǎn)換
            參考如下代碼:
             1#include <stdio.h>
             2#include <iostream>
             3
             4using namespace std;
             5
             6
             7
             8
             9class C 
            10{
            11public:
            12    int i;
            13    C(const C&)   // an  copy constructor
            14    {
            15        printf("\nin the copy constructor");
            16    }

            17    explicit C(int i )   // an explicit constructor
            18    {
            19        printf("\nin the constructor");
            20    }

            21
            22    operator int()const //classs ->int
            23    {
            24        printf("\noperator int()");
            25        return i;
            26    }

            27
            28    C()
            29    {
            30        i = 0;
            31    }

            32}
            ;
            33
            34void test_fun1(C obj);
            35void test_fun2(int i);
            36void test_fun3(C c);
            37
            38
            39int main()
            40{
            41    C c, d;
            42    test_fun3(c);   // c is copied
            43
            44    //test_fun1(1);//錯誤,因為構(gòu)造函數(shù)聲明了explicit,所以禁止隱式轉(zhuǎn)換
            45
            46    test_fun2(c); //call operator int()const
            47    test_fun1(c);
            48    cin.get();
            49    return 0;
            50}

            51
            52void test_fun1(C obj)
            53{
            54}

            55
            56void test_fun2(int i)
            57{
            58}

            59
            60void test_fun3(C c)
            61{
            62}
            從網(wǎng)上看到這段代碼:
             1#include <string>
             2#include <iostream>
             3
             4using namespace std;
             5
             6class Number {
             7    public:
             8        string type; 
             9
            10        Number(): type("void"{ }
            11      explicit   Number(short) : type("short"{ } 
            12        Number(int) : type("int"{ }
            13    }
            ;
            14    void Show(const Number& n) { cout << n.type; }
            15    void f()
            16    {
            17        short s = 42;
            18        Show(s); 
            19    }

            20int main()
            21{
            22f();
            23cin.get();
            24return 0;
            25}

            26
            這段代碼的返回值為:int,這里面起關鍵作用的就是那個explicit


            posted on 2008-06-25 21:36 楊彬彬 閱讀(210) 評論(0)  編輯 收藏 引用 所屬分類: C++語言
            97精品国产97久久久久久免费 | 26uuu久久五月天| 久久99精品国产99久久| 国产精品久久久久影视不卡| 国产毛片久久久久久国产毛片 | 日产精品99久久久久久| 国内精品久久久久久99蜜桃| 国产亚洲色婷婷久久99精品91| 精品国产日韩久久亚洲| 丁香五月网久久综合| 久久精品国产亚洲Aⅴ蜜臀色欲| 久久亚洲精品无码观看不卡| 国内精品久久久久影院亚洲| 香蕉久久影院| 久久电影网2021| 久久人人爽人人人人片av| 久久国产亚洲精品麻豆| 三级三级久久三级久久| 精品久久国产一区二区三区香蕉| 无码久久精品国产亚洲Av影片 | 2021最新久久久视精品爱| 中文字幕成人精品久久不卡| 久久综合给合久久狠狠狠97色 | 日本五月天婷久久网站| 国产精品午夜久久| 91精品国产色综久久| 男女久久久国产一区二区三区| 亚洲一级Av无码毛片久久精品| 国产国产成人精品久久| 欧美日韩精品久久免费| 久久天天日天天操综合伊人av | 91麻豆国产精品91久久久| 久久精品国产精品亚洲| 色综合久久最新中文字幕| 国产精品99久久久久久人| 日本欧美久久久久免费播放网| 久久亚洲AV成人出白浆无码国产| 亚洲欧美伊人久久综合一区二区| 一本一本久久a久久综合精品蜜桃| 久久无码AV一区二区三区| 亚洲国产精品无码久久九九|