欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來到入門教程網(wǎng)!

C語言

當前位置:主頁 > 軟件編程 > C語言 >

探討:C++中函數(shù)返回引用的注意事項

來源:本站原創(chuàng)|時間:2020-01-10|欄目:C語言|點擊: 次

函數(shù) 返回值 和 返回引用 是不同的
函數(shù)返回值時會產(chǎn)生一個臨時變量作為函數(shù)返回值的副本,而返回引用時不會產(chǎn)生值的副本,既然是引用,那引用誰呢?這個問題必須清楚,否則將無法理解返回引用到底是個什么概念。以下是幾種引用情況:
1,引用函數(shù)的參數(shù),當然該參數(shù)也是一個引用

復制代碼 代碼如下:

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

以上函數(shù)的返回值是引用類型。無論返回s1或是s2,調(diào)用函數(shù)和返回結果時,都沒有復制這些string對象。簡單的說,返回的引用是函數(shù)的參數(shù)s1或s2,同樣s1和s2也是引用,而不是在函數(shù)體內(nèi)產(chǎn)生的。函數(shù)體內(nèi)局部對象是不能被因喲個的,因為函數(shù)調(diào)用完局部對象會被釋放。

2,千萬不要返回局部對象的引用
復制代碼 代碼如下:

const string &mainip(const string &s)
      {
             string ret=s;
             return ret;
      }


當函數(shù)執(zhí)行完畢,程序?qū)⑨尫欧峙浣o局部對象的存儲空間。此時,對局部對象的引用就會指向不確定的內(nèi)存。
3,不能返回函數(shù)內(nèi)部定義的對象。在類的成員函數(shù)中,返回引用的類對象,當然不能是函數(shù)內(nèi)定義的類對象(會釋放掉),一般為this指向的對象,典型的例子是string類的賦值函數(shù)。
復制代碼 代碼如下:

<SPAN style="COLOR: #000066; FONT-SIZE: 16px">String& String::operator =(const String &str)  //注意與“+”比較,函數(shù)為什么要用引用呢?a=b=c,可以做為左值
{
 if (this == &str)
 {
  return *this; 
 }
 delete [] m_string;
 int len = strlen(str.m_string);
 m_string = new char[len+1];
 strcpy(m_string,str.m_string);
 return *this;
}</SPAN>

這與sting類中的“+”運算符重載不一樣。“+”運算符的重載不能返回引用,因為它返回的是在函數(shù)內(nèi)定義的類對象,附上代碼。
復制代碼 代碼如下:

<SPAN style="COLOR: #000066; FONT-SIZE: 16px">String String::operator +(const String &str)   
{
 String newstring;
 if (!str.m_string)
 {
  newstring = *this;
 }
 else if (!m_string)
 {
  newstring = str;
 }
 else
 {
  int len = strlen(m_string)+strlen(str.m_string);
  newstring.m_string = new char[len+1];
  strcpy(newstring.m_string,m_string);
  strcat(newstring.m_string,str.m_string);
 }
 return newstring;
}</SPAN>

4,引用返回左值(上例的=賦值也是如此,即a=b=c是可以的)
復制代碼 代碼如下:

 char &get_val(string &str,string::size_type ix)
      {
             return str[ix];
      }
      使用語句調(diào)用:
       string s("123456");
       cout<<s<<endl;
       get_val(s,0)='a';
       cout<<s<<endl;

最后轉(zhuǎn)上一段code作為總結。
復制代碼 代碼如下:

<span style="font-size:16px;color:#000066;">#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];
}</span>

網(wǎng)頁制作CMS教程網(wǎng)絡編程軟件編程腳本語言數(shù)據(jù)庫服務器

如果侵犯了您的權利,請與我們聯(lián)系,我們將在24小時內(nèi)進行處理、任何非本站因素導致的法律后果,本站均不負任何責任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權所有