如何理解引用作为函数的返回值?
- 1.引用作为函数的返回值时,必须在定义函数时在函数名前将&
- 2.用引用作函数的返回值的最大的好处是在内存中不产生返回值的副本
//代码来源:RUNOOB
#include<iostream>
using namespace std;
float temp;
float fn1(float r){
temp=r*r*3.14;
return temp;
}
float &fn2(float r){ //&说明返回的是temp的引用,换句话说就是返回temp本身
temp=r*r*3.14;
return temp;
}
int main(){
float a=fn1(5.0); //case 1:返回值
//float &b=fn1(5.0); //case 2:用函数的返回值作为引用的初始化值 [Error] invalid initialization of non-const reference of type 'float&' from an rvalue of type 'float'
//(有些编译器可以成功编译该语句,但会给出一个warning)
float c=fn2(5.0);//case 3:返回引用
float &d=fn2(5.0);//case 4:用函数返回的引用作为新引用的初始化值
cout<<a<<endl;//78.5
//cout<<b<<endl;//78.5
cout<<c<<endl;//78.5
cout<<d<<endl;//78.5
return 0;
}
case 1:用返回值方式调用函数:
返回全局变量temp的值时,C++会在内存中创建临时变量并将temp的值拷贝给该临时变量。当返回到主函数main后,赋值语句a=fn1(5.0)会把临时变量的值再拷贝给变量a
case 2:用函数的返回值初始化引用的方式调用函数
这种情况下,函数fn1()是以值方式返回到,返回时,首先拷贝temp的值给临时变量。返回到主函数后,用临时变量来初始化引用变量b,使得b成为该临时变量到的别名。由于临时变量的作用域短暂(在C++标准中,临时变量或对象的生命周期在一个完整的语句表达式结束后便宣告结束,也就是在语句float &b=fn1(5.0);之后) ,所以b面临无效的危险,很有可能以后的值是个无法确定的值。
case 3:用返回引用的方式调用函数:
这种情况下,函数fn2()的返回值不产生副本,而是直接将变量temp返回给主函数,即主函数的赋值语句中的左值是直接从变量temp中拷贝而来(也就是说c只是变量temp的一个拷贝而非别名) ,这样就避免了临时变量的产生。尤其当变量temp是一个用户自定义的类的对象时,这样还避免了调用类中的拷贝构造函数在内存中创建临时对象的过程,提高了程序的时间和空间的使用效率
case 4:用函数返回的引用作为新引用的初始化值的方式来调用函数:
这种情况下,函数fn2()的返回值不产生副本,而是直接将变量temp返回给主函数。在主函数中,一个引用声明d用该返回值初始化,也就是说此时d成为变量temp的别名。由于temp是全局变量,所以在d的有效期内temp始终保持有效,故这种做法是安全的。
那么什么情况下要返回对象的引用呢?
- 允许进行连续赋值
- 防止返回对象(返回对象也可以进行连续赋值(常规的情况,如a = b = c,而不是(a = b) = c))的时候调用拷贝构造函数和析构函数导致不必要的开销,降低赋值运算符的效率。
下面看一个赋值运算符重载的例子:(连续赋值,常规的情况(a = b = c))
#include <iostream>
using namespace std;
class String
{
private:
char *str;
int len;
public:
String(const char* s);//构造函数声明
String operator=(const String& another);//运算符重载,此时返回的是对象
void show()
{
cout << "value = " << str << endl;
}
/*copy construct*/
String(const String& other)
{
len = other.len;
str = new char[len + 1];
strcpy(str, other.str);
cout << "copy construct" << endl;
}
~String()
{
delete[] str;
cout << "deconstruct" << endl;
}
};
String::String(const char* s)//构造函数定义
{
len = strlen(s);
str = new char[len + 1];
strcpy(str, s);
}
String String::operator=(const String &other)//运算符重载
{
if (this == &other)
return *this;
// return;
delete[] str;
len = other.len;
str = new char[len + 1];
strcpy(str, other.str);
return *this;
// return;
}
int main()
{
String str1("abc");
String str2("123");
String str3("456");
str1.show();
str2.show();
str3.show();
str3 = str1 = str2;//str3.operator=(str1.operator=(str2))
str3.show();
str1.show();
return 0;
}
1.直接运行结果:
2.下面是返回引用的情况(String& operator = (const String& str)),直接贴运行结果
当运算符重载返回的是对象时,会在连续赋值运算过程的返回途中,调用两次拷贝构造函数和析构函数(因为return的是个新的对象)
如果采用String& operator = (const String& str)这样就不会有多余的调用(因为这里直接return一个已经存在的对象的引用)
上面的栗子也说明一点:析构函数的调用是在变量作用域结束的时候(以及程序运行结束的时候)如果采用return对象,那么第二次赋值运算调用的情况就是:将一个新的String对象(returnStringObj)传递到operator = (const String& str)的参数中去 相当于
const String&str = returnStringObj;
如果采用return对象引用,那么第二次赋值运算的情况就是:将一个已经存在的String对象的引用((其实就是str1))传递给operator = (const String& str)的参数中去
const String&str = returnReference; //(String& returnReference =
str1;)