The function return value and return reference are different
When a function returns a value, it produces a temporary variable as a copy of the value returned by the function, and a copy of the value is not generated when the reference is returned, since it is a reference, who does it refer to? This question must be clear, otherwise it will be impossible to understand what the return reference really is. Here are several references:
1, reference the parameters of the function, of course, this parameter is also a reference
Copy Code code as follows:
Const string &shorterstring (const string &s1,const string &s2)
{
Return S1.size () <s2.size () s1:s2;
}
The return value of the above function is a reference type. If you return S1 or S2, you do not copy these string objects when calling functions and returning results. Simply put, the returned reference is the parameter S1 or S2 of the function, and the same S1 and S2 are references, not generated in the function body. Local objects in a function body cannot be caused by yo because the function calls the local object to be freed.
2, never return a reference to a local object
Copy Code code as follows:
Const string &mainip (const string &s)
{
String ret=s;
return ret;
}
When the function completes, the program releases the storage space that is assigned to the local object. At this point, a reference to the local object will point to an indeterminate memory.
3, you cannot return an object defined within a function. In a member function of a class, the referenced class object is returned, certainly not the class object defined within the function (which is released), typically the object to which this is directed, and the typical example is the assignment function of the string class.
Copy Code code as follows:
<span style= "COLOR: #000066; Font-size:16px ">String& string::operator = (const String &STR)//note compared to" + ", why do functions use references? A=b=c, it can be left as a value
{
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>
This is not the same as the "+" operator overload in the Sting class. Overloading of the "+" operator cannot return a reference because it returns a class object defined within the function, with code attached.
Copy Code code as follows:
<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, the reference returns the left value (this is the same as the = assignment in the example above, that is, a=b=c is possible)
Copy Code code as follows:
Char &get_val (String &str,string::size_type ix)
{
return Str[ix];
}
Using statement calls:
String S ("123456");
cout<<s<<endl;
Get_val (s,0) = ' a ';
cout<<s<<endl;
Finally, turn on a section of code as a summary.
Copy Code code as follows:
<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;
}
Return non-reference
String Make_plural (size_t i,const string &word,const string &ending)
{
Return (i==1) word:word+ending;
}
return reference
Const string &shorterstring (const string &s1,const string &s2)
{
Return S1.size () <s2.size () s1:s2;
}
Prohibit returning a reference to a local object (my dev C + + is not an error, it's scary)
Const string &mainip (const string &s)
{
String ret=s;
return ret;
}
Reference returns the left value
Char &get_val (String &str,string::size_type ix)
{
return Str[ix];
}</span>