• <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>
            隨筆-60  評論-111  文章-0  trackbacks-0
            只知道 一個函數 如果返回值是一個對象的話 肯定是要涉及到copy construct的 還會構造臨時對象 那如果返回的是引用呢?
            帶著n多的疑問?寫了下面的代碼 來解惑

            ?1#include?<iostream>
            ?2
            ?3class?TestClass
            ?4{
            ?5private:
            ?6????static?int?cnt;
            ?7????int?idx;
            ?8????int?age;
            ?9public:
            10????int?GetAge(){return?age;}
            11????TestClass(int?n)
            12????{
            13????????idx=cnt;
            14????????cnt++;
            15
            16????????age=n;
            17????????std::cout<<idx<<"?TestClass(int?n)"<<std::endl;
            18????}

            19????TestClass()
            20????{
            21????????idx=cnt;
            22????????cnt++;
            23
            24????????age=-1;
            25????????std::cout<<idx<<"?TestClass()"<<std::endl;
            26????}

            27????TestClass(const?TestClass&?v)
            28????{
            29????????idx=cnt;
            30????????cnt++;
            31
            32????????age=v.age;
            33????????std::cout<<idx<<"?TestClass(const?TestClass&?v)"<<std::endl;
            34????}

            35????~TestClass()
            36????{
            37????????std::cout<<idx<<"?~TestClass()"<<std::endl;
            38????}

            39????const?TestClass&?operator=?(const?TestClass&?v)
            40????{
            41????????age=v.age;
            42????????std::cout<<idx<<"?operator="<<std::endl;
            43????????return?*this;
            44????}

            45}
            ;
            46
            47int?TestClass::cnt=0;
            48
            49TestClass?Get()
            50{
            51????TestClass?l(1);
            52????return?l;
            53}

            54
            55TestClass?Get2()
            56{
            57????return?TestClass(2);
            58}

            59
            60TestClass&?Get3()
            61{
            62????TestClass?l(3);
            63????return?l;
            64}

            65
            66TestClass&?Get4()
            67{
            68????return?TestClass(4);
            69}

            70
            71void?test(void)
            72{
            73????TestClass?t;
            74????std::cout<<"\t0"<<"\tAge="<<t.GetAge()<<std::endl;
            75????t=Get();
            76????std::cout<<"\t1"<<"\tAge="<<t.GetAge()<<std::endl;
            77????t=Get2();
            78????std::cout<<"\t2"<<"\tAge="<<t.GetAge()<<std::endl;
            79????t=Get3();
            80????std::cout<<"\t3"<<"\tAge="<<t.GetAge()<<std::endl;
            81????t=Get4();
            82????std::cout<<"\t4"<<"\tAge="<<t.GetAge()<<std::endl;
            83}

            84
            85int?main(int?argc,?char*?argv[])
            86{
            87????test();
            88????system("pause");
            89????return?0;
            90}

            91

            代碼輸出是這樣的


            顯然Get3&Get4是不能得到正確的結果的,因為函數返回時,局部對象的生存期就結束了,是對象的話就被解構了,所以返回的引用也不能對應正確的對象

            Get2比Get少構造一個對象,當計算不是很復雜時Get2顯然是最高效滴
            posted on 2006-09-05 03:08 shaker(太子) 閱讀(595) 評論(3)  編輯 收藏 引用 所屬分類: C++

            評論:
            # re: 函數返回對象各種方法,怎樣才是最高效? 2006-09-05 17:50 | chenger
            現在的編譯器都實現了返回值優化,一定程度上能減少拷貝次數  回復  更多評論
              
            # re: 函數返回對象各種方法,怎樣才是最高效? 2006-09-05 17:54 | 周星星
            “ 一個函數 如果返回值是一個對象的話 肯定是要涉及到copy construct的 還會構造臨時對象”
            ------ 在google上搜索一下 rvo 和 nrvo,或者直接看C++標準文檔,不過那花費時間:)  回復  更多評論
              
            # re: 函數返回對象各種方法,怎樣才是最高效? 2006-09-06 03:24 | shaker
            謝謝指點  回復  更多評論
              
            国产精品va久久久久久久| 国产精品日韩深夜福利久久| 国产精品美女久久久久久2018| 国产精品久久久久影视不卡| 亚洲AV无码久久精品蜜桃| 国产亚洲精久久久久久无码AV| 久久精品无码一区二区三区免费| 久久强奷乱码老熟女网站| 亚洲综合精品香蕉久久网| 日韩精品久久无码中文字幕| 久久精品国产久精国产思思| 国产精品久久影院| 久久99亚洲综合精品首页| 中文精品99久久国产 | 99久久无色码中文字幕| 青青草国产精品久久久久| 久久久久国产成人精品亚洲午夜| 亚洲国产精品成人久久蜜臀| 四虎亚洲国产成人久久精品| 国产精品美女久久久m| 久久最新免费视频| 久久久无码人妻精品无码| 9久久9久久精品| 久久精品国产久精国产果冻传媒| 国产成人精品久久一区二区三区av| 色妞色综合久久夜夜| 99久久精品免费观看国产| 亚洲国产小视频精品久久久三级| 狠狠久久亚洲欧美专区| 久久AV无码精品人妻糸列| 国产精品午夜久久| 久久久精品国产sm调教网站 | 国产日韩久久免费影院| 久久久久亚洲av无码专区喷水 | 少妇久久久久久被弄高潮| 久久精品国产亚洲av麻豆蜜芽| 国产精品久久新婚兰兰| 久久国产欧美日韩精品免费| 无夜精品久久久久久| 性欧美大战久久久久久久| 久久久www免费人成精品|