一、函数返回值和返回引用是不同的
add函数中,sum是个局部变量,函数运行结束,变量也被销毁,但是编译器会创建一个没有命名的对象(临时对象)来存储这个计算结果,成功的赋值给了调用该函数的变量。
形参和返回类型都是指向 const string 对象的引用,调用函数和返回结果时,都没有复制这些 string 对象。
给函数返回值赋值可能让人惊讶,由于函数返回的是一个引用,因此这是正确的,该引用是被返回元素的同义词。
如果不希望引用返回值被修改,返回值应该声明为 const。
采用非引用的string类型作为返回值就没有问题。
函数返回值时会产生一个临时变量作为函数返回值的副本,而返回引用时不会产生值的副本。
1.普通函数返回值
C++ primer中这样写:函数的返回值用于初始化在调用函数处创建的临时对象。在求解表达式时,如果需要一个地方储存其运算结果,编译器会创建一个没有命名的对象,这就是临时对象。
例如:
#include<iostream>
using namespace std;
int add(int i,int j)
{
int sum=i+j;
return sum;
}
int main()
{
int sum=add(1,2);
cout<<sum<<endl;
system("pause");
return 0;
}
add函数中,sum是个局部变量,函数运行结束,变量也被销毁,但是编译器会创建一个没有命名的对象(临时对象)来存储这个计算结果,成功的赋值给了调用该函数的变量。
string make_plural(size_t ctr, const string &word,
const string &ending)
{
return (ctr == 1) ? word : word + ending;
}
这个函数要么返回其形参 word 的副本,要么返回一个未命名的临时string 对象,这个临时对象是由字符串 word 和 ending 的相加而产生的。这两种情况下,return 都在调用该函数的地方复制了返回的 string 对象。
2.普通函数返回引用
(1)当函数返回引用类型时,没有复制返回值。相反,返回的是对象本身。
// find longer of two strings
const string &shorterString(const string &s1, const string &s2)
{
return s1.size() < s2.size() ? s1 : s2;
}
形参和返回类型都是指向 const string 对象的引用,调用函数和返回结果时,都没有复制这些 string 对象。
简单的说,返回的引用是函数的参数s1或s2,同样s1和s2也是引用,而不是在函数体内产生的。函数体内局部对象是不能被引用的,因为函数调用完局部对象会被释放。
(2)千万不要返回局部对象的引用(返回引用时并不创建临时对象)
当函数执行完毕时,将释放分配给局部对象的存储空间。此时,对局部对象的引用就会指向不确定的内存。
// Disaster: Function returns a reference to a local object
const string &manip(const string& s)
{
string ret = s;
// transform ret in some way
return ret; // Wrong: Returning reference to a local object!
}
这个函数会在运行时出错,因为它返回了局部对象的引用。当函数执行完毕,字符串 ret 占用的储存空间被释放,函数返回值指向了对于这个程序来说不再有效的内存空间。
3.类的成员函数返回引用
在类的成员函数中,返回引用的类对象,当然不能是函数内定义的类对象(会释放掉),一般为this指向的对象,典型的例子是string类的赋值函数。
String & String::operator=(const String &other) //<span style="color: rgb(0, 130, 0); font-family: Consolas, 'Courier New', Courier, mono, serif; line-height: 18px;">注意与“+”比较,函数为什么要用引用呢?a=b=c,可以做为左值</span>
{
if(&other==this)
return *this;
delete []m_data;
int length=strlen_m(other.m_data);
m_data=new char[length+1];
strcpy_m(m_data,other.m_data);
return *this;
}
这与sting类中的“+”运算符重载不一样。“+”运算符的重载不能返回引用,因为它返回的是在函数内定义的类对象.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;
}
4.引用返回左值
返回引用的函数返回一个左值。因此,这样的函数可用于任何要求使用左值的地方:
#include<iostream>
#include<string>
using namespace std;
char &change_char(string &str,int x)
{
return str[x];
}
int main()
{
string str="lanzhihui";
cout<<str<<endl;
cout<<change_char(str,1)<<endl;
change_char(str,1)='A';
cout<<str<<endl;
system("pause");
return 0;
}
给函数返回值赋值可能让人惊讶,由于函数返回的是一个引用,因此这是正确的,该引用是被返回元素的同义词。
如果不希望引用返回值被修改,返回值应该声明为 const。
5.千万不要返回指向局部对象的指针
函数的返回类型可以是大多数类型。特别地,函数也可以返回指针类型。和返回局部对象的引用一样,返回指向局部对象的指针也是错误的。一旦函数结束,局部对象被释放,返回的指针就变成了指向不再存在的对象的悬垂指针。
#include<iostream>
#include<string>
using namespace std;
char *fun()
{
char *p="Hello,World";
return p;
}
int main()
{
char *p=NULL;
p=fun();
cout<<p<<endl;//p中存储的是垃圾内容,不是你想要的内容
system("pause");
return 0;
}
采用非引用的string类型作为返回值就没有问题。
#include<iostream>
#include<string>
using namespace std;
string fun()
{
char *p="Hello,World";
return p;
}
int main()
{
string p;
p=fun();
cout<<p<<endl;
system("pause");
return 0;
}