探討: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í)行完畢,程序?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>
您可能感興趣的文章
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言中對數(shù)函數(shù)的表達式 c語言中對數(shù)怎么表達
- 04-02c語言沒有round函數(shù) round c語言
- 04-02C語言中怎么打出三角函數(shù) c語言中怎么打出三角函數(shù)的值
- 01-10深入理解C++中常見的關鍵字含義
- 01-10使用C++實現(xiàn)全排列算法的方法詳解
- 01-10深入Main函數(shù)中的參數(shù)argc,argv的使用詳解
- 01-10APUE筆記之:進程環(huán)境詳解
- 01-10c++中inline的用法分析
- 01-10如何尋找數(shù)組中的第二大數(shù)


閱讀排行
本欄相關
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言的正則匹配函數(shù) c語言正則表達
- 04-02c語言用函數(shù)寫分段 用c語言表示分段
- 04-02c語言中對數(shù)函數(shù)的表達式 c語言中對
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數(shù) c語言中怎
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求
隨機閱讀
- 01-11ajax實現(xiàn)頁面的局部加載
- 01-10SublimeText編譯C開發(fā)環(huán)境設置
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-10C#中split用法實例總結
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 04-02jquery與jsp,用jquery
- 01-10delphi制作wav文件的方法