• <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>
            posts - 311, comments - 0, trackbacks - 0, articles - 0
              C++博客 :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理
            返回非引用

                  string make_plural(size_t i,const string &word,const string &ending)
                  {
                         return (i==1)?word:word+ending;
                  }

                  以上函數,當i等于1時,函數返回word形參的副本;當i不等于1時函數返回一個臨時的string對象,這個臨時對象是由字符串word和ending相加而成的。這兩種情況下,return都在調用該函數的地方防復制了返回的string對象。

            返回引用

                  const string &shorterString(const string &s1,const string &s2)
                  {
                         return s1.size()<s2.size()?s1:s2;
                  }

                  以上函數的返回值是引用類型。無論返回s1或是s2,調用函數和返回結果時,都沒有復制這些string對象。

            千萬不要返回局部對象的引用

                  const string &mainip(const string &s)
                  {
                         string ret=s;
                         return ret;
                  }
                  
                  當函數執行完畢,程序將釋放分配給局部對象的存儲空間。此時,對局部對象的引用就會指向不確定的內存。(我在dev c++里運行通過。。。。 但是vc6和vc2008不能編譯通過)。同理,也不能返回局部對象的指針。
                  

            引用返回左值(比較奇妙)

                  char &get_val(string &str,string::size_type ix)
                  {
                         return str[ix];
                  }

                  使用語句調用:
                   string s("123456");
                   cout<<s<<endl;
                   get_val(s,0)='a';
                   cout<<s<<endl;
                  把函數應用于左值,還是第一次見到(真是孤陋寡聞)。以下是上面幾種情況的實例代碼:
            #include<iostream>
            using namespace std;
            string make_plural(size_t,const string&,const string&);
            const string &shorterString(const string &,const string &);
            const string &mainip(const string&);
            char &get_val(string &,string::size_type);
            int main(void)
            {
                cout
            <<make_plural(1,"dog","s")<<endl;
                cout
            <<make_plural(2,"dog","s")<<endl;
                
                
            string string1="1234";
                
            string string2="abc";
                cout
            <<shorterString(string1,string2)<<endl;
                
                cout
            <<mainip("jiajia")<<endl;
                
                
                
            string s("123456");
                cout
            <<s<<endl;
                get_val(s,
            0)='a';
                
                cout
            <<s<<endl;
                
                getchar();
                
            return 0;
            }
            //返回非引用 
            string make_plural(size_t i,const string &word,const string &ending)
            {
                
            return (i==1)?word:word+ending;
            }
            //返回引用 
            const string &shorterString(const string &s1,const string &s2)
            {
                
            return s1.size()<s2.size()?s1:s2;
            }
            //禁止返回局部對象的引用(我的dev c++ 沒有報錯,比較可怕) 
            const string &mainip(const string &s)
            {
                
            string ret=s;
                
            return ret;
            }
            //引用返回左值(第一次聽說) 
            char &get_val(string &str,string::size_type ix)
            {
                
            return str[ix];
            }

            久久国产免费观看精品| 亚洲精品乱码久久久久久蜜桃| 久久天天躁狠狠躁夜夜avapp | 91精品无码久久久久久五月天| 久久综合香蕉国产蜜臀AV| 国产成人综合久久综合| 久久久久久久久久免免费精品| 久久亚洲日韩看片无码| 狠色狠色狠狠色综合久久| 99久久亚洲综合精品网站| 久久亚洲sm情趣捆绑调教| 久久久久久a亚洲欧洲aⅴ| 久久久一本精品99久久精品88| 久久99久久99小草精品免视看 | 久久久久婷婷| 99999久久久久久亚洲| 久久久久久久免费视频| 国产精品热久久无码av| 久久综合狠狠综合久久| 伊人伊成久久人综合网777| 国产成人精品久久免费动漫| 久久人做人爽一区二区三区| 狠狠精品干练久久久无码中文字幕 | 久久精品国产亚洲综合色| 亚洲中文久久精品无码| 久久午夜综合久久| 国产综合成人久久大片91| 香蕉久久夜色精品国产小说| 婷婷久久香蕉五月综合加勒比| 日批日出水久久亚洲精品tv| 国产激情久久久久影院小草| 996久久国产精品线观看| 亚洲乱码中文字幕久久孕妇黑人 | 色综合久久综合网观看| 久久天天躁狠狠躁夜夜96流白浆| 午夜福利91久久福利| 亚洲?V乱码久久精品蜜桃| 久久996热精品xxxx| 久久久久综合国产欧美一区二区| 岛国搬运www久久| 99久久国产综合精品五月天喷水|