• <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年5月>
            27282930123
            45678910
            11121314151617
            18192021222324
            25262728293031
            1234567

            常用鏈接

            留言簿(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 楊彬彬 閱讀(202) 評論(0)  編輯 收藏 引用 所屬分類: C++語言
            狠狠色丁香久久婷婷综| 国产精品久久久久国产A级| 久久人妻少妇嫩草AV蜜桃| 无码人妻久久一区二区三区蜜桃| 国产99久久久国产精免费| 欧美精品九九99久久在观看| 99久久精品国内| 中文字幕久久精品| 久久午夜免费视频| 99久久精品日本一区二区免费| 国产午夜精品理论片久久| 久久成人国产精品免费软件| 久久精品国产亚洲av高清漫画| 国产精品九九久久精品女同亚洲欧美日韩综合区 | 色综合久久久久| 无码乱码观看精品久久| 亚洲国产精品久久久久婷婷软件 | 亚洲色婷婷综合久久| 精品久久久久久久久久中文字幕| 亚洲精品无码久久千人斩| 亚洲精品乱码久久久久久蜜桃| 99久久精品日本一区二区免费| 久久99热这里只有精品66| 久久久精品波多野结衣| 91精品日韩人妻无码久久不卡 | 午夜视频久久久久一区 | www.久久热| 激情伊人五月天久久综合| 伊人色综合久久天天人手人婷| 天堂无码久久综合东京热| 日韩电影久久久被窝网| 久久人人爽人人精品视频| 国产精品内射久久久久欢欢| 久久青青草原国产精品免费| 久久久久免费看成人影片| 日韩精品久久久久久免费| 久久免费的精品国产V∧| 久久久久亚洲av无码专区导航| 久久丫精品国产亚洲av不卡| 亚洲国产精品久久久天堂| 欧美亚洲色综久久精品国产 |