函数返回值和返回引用的 区别

函数返回时会产生一个临时变量作为函数返回值的副本,而返回引用时不会产生值的副本,既然是引用,那引用谁呢?这个问题必须清楚,否则将无法理解返回引用到底是个什么概念。以下是几种引用情况:

1,引用函数的参数,当然该参数(s1、s2)也是一个引用

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

   以上函数的返回值是引用类型。无论返回s1或是s2,调用函数和返回结果时,都没有复制这些string对象。简单的说,返回的引用是函数的参数s1或s2,同样s1和s2也是引用,而不是在函数体内产生的。函数体内局部对象是不能被因哟个的,因为函数调用完局部对象会被释放。

解释2:通常的返回机制将返回值复制到临时存储区域中, 随后调用程序将访问该区域. 
返回引用则程序则调用程序将直接访问返回值.
通常引用将指向传递给函数的引用, 因此调用函数实际上是直接访问自己的一个变量.
比如
const int& fun (int& a, const int& b)
{
    a = a + b;
    return a;
}
int x = 1, y = 2, z;
z = fun(x, y);
//等价于 fun(x, y); z = x;

 

2,千万不要返回局部对象的引用

 const string &mainip(const string &s)
      {
             string ret=s;
             return ret;
      }
      当函数执行完毕,程序将释放分配给局部对象的存储空间。此时,对局部对象的引用就会指向不确定的内存。

3、在类的成员函数中,返回引用的类对象,当然不能是函数内定义的类对象(会释放掉),一般为this指向的对象,典型的例子是string类的赋值函数。

1.String& String::operator =(const String &str)  //注意与“+”比较,函数为什么要用引用呢?a=b=c,可以做为左值  
2.{  
3.    if (this == &str)  
4.    {  
5.        return *this;    
6.    }  
7.    delete [] m_string;  
8.    int len = strlen(str.m_string);  
9.    m_string = new char[len+1];  
10.    strcpy(m_string,str.m_string);  
11.    return *this;  
12.}  


这与sting类中的“+”运算符重载不一样。“+”运算符的重载不能返回引用,因为它返回的是在函数内定义的类对象,附上代码。

 

1.String String::operator +(const String &str)      
2.{  
3.    String newstring;  
4.    if (!str.m_string)  
5.    {  
6.        newstring = *this;  
7.    }  
8.    else if (!m_string)  
9.    {  
10.        newstring = str;  
11.    }  
12.    else  
13.    {  
14.        int len = strlen(m_string)+strlen(str.m_string);  
15.        newstring.m_string = new char[len+1];  
16.        strcpy(newstring.m_string,m_string);  
17.        strcat(newstring.m_string,str.m_string);  
18.    }  
19.    return newstring;  
20.}  

4,引用返回左值(上例的=赋值也是如此,即a=b=c是可以的)

      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;

最后转上一段code作为总结。

 

1.#include<iostream>  
2.using namespace std;  
3.string make_plural(size_t,const string&,const string&);  
4.const string &shorterString(const string &,const string &);  
5.const string &mainip(const string&);  
6.char &get_val(string &,string::size_type);  
7.int main(void)  
8.{  
9.    cout<<make_plural(1,"dog","s")<<endl;  
10.    cout<<make_plural(2,"dog","s")<<endl;  
11.      
12.    string string1="1234";  
13.    string string2="abc";  
14.    cout<<shorterString(string1,string2)<<endl;  
15.      
16.    cout<<mainip("jiajia")<<endl;  
17.      
18.      
19.    string s("123456");  
20.    cout<<s<<endl;  
21.    get_val(s,0)='a';  
22.      
23.    cout<<s<<endl;  
24.      
25.    getchar();  
26.    return 0;  
27.}  
28.//返回非引用   
29.string make_plural(size_t i,const string &word,const string &ending)  
30.{  
31.    return (i==1)?word:word+ending;  
32.}  
33.//返回引用   
34.const string &shorterString(const string &s1,const string &s2)  
35.{  
36.    return s1.size()<s2.size()?s1:s2;  
37.}  
38.//禁止返回局部对象的引用(我的dev c++ 没有报错,比较可怕)   
39.const string &mainip(const string &s)  
40.{  
41.    string ret=s;  
42.    return ret;  
43.}  
44.//引用返回左值  
45.char &get_val(string &str,string::size_type ix)  
46.{  
47.    return str[ix];  
48.}  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值