• <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關鍵字聲明的構造函數禁止編譯器進行隱式類型轉換 .(禁止單參數構造函數"被用于自動型別轉換
            參考如下代碼:
             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);//錯誤,因為構造函數聲明了explicit,所以禁止隱式轉換
            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}
            從網上看到這段代碼:
             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++語言
            www.久久热.com| 精品国际久久久久999波多野| 久久婷婷五月综合97色一本一本 | 久久综合九色综合网站| 亚洲国产精品无码久久久蜜芽| 九九久久自然熟的香蕉图片| 精品免费久久久久久久| 91精品国产高清久久久久久国产嫩草| 热久久国产精品| 无码精品久久久天天影视| 久久精品国产免费| 亚洲国产精品无码久久| 精品久久777| 18禁黄久久久AAA片| 亚洲精品无码久久久影院相关影片| 欧美国产成人久久精品| 国产午夜精品理论片久久影视| 国产精品午夜久久| 99久久99久久久精品齐齐| 国产精品成人久久久久久久| 久久久久免费精品国产| 午夜精品久久久久成人| 国产精自产拍久久久久久蜜| 狠狠精品久久久无码中文字幕| 精品国产日韩久久亚洲| 亚洲成色999久久网站| 亚洲va久久久噜噜噜久久天堂| 99久久精品无码一区二区毛片 | 亚洲AV无码久久精品色欲| 品成人欧美大片久久国产欧美| 久久婷婷激情综合色综合俺也去| 亚洲午夜久久久| 人人妻久久人人澡人人爽人人精品| 久久久久久久综合日本亚洲| 久久久老熟女一区二区三区| 久久精品www人人爽人人| 久久亚洲熟女cc98cm| 久久婷婷人人澡人人爽人人爱 | 国产成人精品综合久久久| 久久综合色区| 青青草原精品99久久精品66|