• <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(太子) 閱讀(587) 評論(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
            謝謝指點  回復  更多評論
              
            天天影视色香欲综合久久| 久久久久亚洲AV无码网站| 久久精品国产网红主播| 99久久婷婷国产综合精品草原 | 久久福利青草精品资源站| 久久精品国产亚洲AV不卡| 久久精品人人做人人爽电影 | 国产午夜福利精品久久2021 | 国产精品99久久久久久www| 国产三级精品久久| 久久亚洲国产精品成人AV秋霞 | 亚洲精品无码成人片久久| 久久福利青草精品资源站免费| 91久久国产视频| 午夜精品久久久久久中宇| 99re久久精品国产首页2020| 国内精品伊人久久久久AV影院| 久久精品国产91久久麻豆自制| 久久中文骚妇内射| 色偷偷88欧美精品久久久| 久久久久综合中文字幕| 久久婷婷五月综合国产尤物app| 免费观看成人久久网免费观看| 性做久久久久久久久| 久久人人爽人人澡人人高潮AV | 77777亚洲午夜久久多人| 久久成人国产精品一区二区| 久久久久无码精品| 久久人搡人人玩人妻精品首页| 亚洲精品NV久久久久久久久久| 久久综合九色综合久99| 日本精品久久久久中文字幕8| yy6080久久| 国产精品久久久福利| 久久精品成人免费观看97| 三级片免费观看久久| 婷婷久久精品国产| 久久久久亚洲AV无码去区首| 久久精品国产亚洲αv忘忧草 | 久久国产色AV免费观看| 狠狠色噜噜色狠狠狠综合久久|