探討:C++中函數(shù)返回引用的注意事項
更新時間:2013年05月29日 10:10:17 作者:
本篇文章是對C++中函數(shù)返回引用的注意事項進行了詳細的分析介紹,需要的朋友參考下
函數(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í)行完畢,程序將釋放分配給局部對象的存儲空間。此時,對局部對象的引用就會指向不確定的內(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;
最后轉上一段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>
函數(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í)行完畢,程序將釋放分配給局部對象的存儲空間。此時,對局部對象的引用就會指向不確定的內(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;
最后轉上一段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>
相關文章
C++ Qt開發(fā)之ComboBox下拉組合框組件用法詳解
Qt 是一個跨平臺C++圖形界面開發(fā)庫,利用Qt可以快速開發(fā)跨平臺窗體應用程序,在Qt中,ComboBox(組合框)是一種常用的用戶界面控件,它提供了一個下拉列表,允許用戶從預定義的選項中選擇一個,本文給大家介紹QComboBox類的一些常用方法,需要的朋友可以參考下2023-12-12C語言數(shù)據(jù)在內(nèi)存中的存儲流程深入分析
使用編程語言進行編程時,需要用到各種變量來存儲各種信息。變量保留的是它所存儲的值的內(nèi)存位置。這意味著,當您創(chuàng)建一個變量時,就會在內(nèi)存中保留一些空間。您可能需要存儲各種數(shù)據(jù)類型的信息,操作系統(tǒng)會根據(jù)變量的數(shù)據(jù)類型,來分配內(nèi)存和決定在保留內(nèi)存中存儲什么2022-10-10C++實現(xiàn)LeetCode(123.買股票的最佳時間之三)
這篇文章主要介紹了C++實現(xiàn)LeetCode(123.買股票的最佳時間之三),本篇文章通過簡要的案例,講解了該項技術的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下2021-07-07C++實現(xiàn)LeetCode(140.拆分詞句之二)
這篇文章主要介紹了C++實現(xiàn)LeetCode(140.拆分詞句之二),本篇文章通過簡要的案例,講解了該項技術的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下2021-07-07