Reference parameters and reference return values

Source: Internet
Author: User

Original post address: http://www.cnblogs.com/bigshow/archive/2008/11/10/1330514.html

 

 

I often see such a statement: T & func (T & t). What is the difference between this statement and T func (T t? The book explains how to improve efficiency? What operations are performed internally? This article uses eight small examples to thoroughly investigate the referenced parameters and returned results.
First, let's take a look at the reference parameters and reference return values in the member functions of the class:

Class Definition

Class
{
Public:
Int x;

A () {}// Constructor
A (const A & other) // copy the constructor
{
This-> x = other. x;
Cout <"Copy" <endl;
}

~ A () {}// destructor

A & operator = (const A & other) // value assignment function
{
This-> x = other. x;
Cout <"Assign" <Endl;
Return * this;
}

Void func1 ()
{

}

Void func2 (A &)
{

}

A func3 ()
{
Return * this;
}
 
A & func4 ()
{
Return * this;
}
};

 

 

This class is very simple. There is only one member variable x, and the default constructor, copy constructor, destructor, and value assignment function are defined. To see more clearly which copy constructor and value assignment function are called, some output information is added to these two functions.
Class also defines four member functions. The following analyzes the execution of these four functions respectively.
(1) Call func1 () in the main () function ():

Call func1 ()

Int main ()
{
A a1, a2;
A2.func1 (a1 );

Return 0;
}

 


Func1 () output result

Copy

Why is this output? This is because the value parameter is passed in func1 (), so before executing the function body

Generate a temporary object first, then call the Copy constructor of the class to initialize the temporary object, and output "Copy ". This temporary object is operated in the function. Any modifications made to the temporary object will not be reflected in the real parameters of the function.

(2) Call func2 () in the main () function to compare it with func1:


Call func2 ()

Int main ()
{
A a1, a2;
A2.func2 (a1 );

Return 0;
}

 

Func2 () output result

 

No output is returned.
This is because a reference parameter is passed in, so there is no need to generate a temporary object inside the function to save the object information,

Therefore, the copy constructor is not called. This is the function of referencing parameters, reducing the copy of an object and improving the function efficiency.

(3) Call func3 () in the main () function ():

Call func3 ()

Int main ()
{
A a1, a2;
A2 = a1.func3 ();

Return 0;
}

 

Func3 () output result

Copy
Assign

 

 

Why is "Copy" output? This is because the function uses value return. To save the returned value, you must first create a temporary object, and then call the copy constructor of the class to copy the * this content to this temporary object, then return the temporary object. Finally, the content of the temporary object is assigned to the new object through the value assignment function.

(4) Call func4 () in the main () function to compare it with func3:

Call func4 ()

Int main ()
{
A a1, a2;
A2 = a1.func4 ();

Return 0;
}

 

Func4 () output result

Assign

 

 

Only the value assignment function is called. this is the reference function that uses the reference return function. Therefore, the reference * this of the object itself is directly returned. No temporary object needs to be created to save the object information, therefore, the copy constructor is not called. Finally, the content of the object is directly assigned to the new object through the value assignment function. This is the function of referencing the return value, which reduces the copy of an object and improves the function efficiency.

Summary:In the member functions of the class, no temporary objects need to be generated when reference parameters and reference return values are used.Object copying improves the function efficiency.

So what will happen if the parameter is returned as a return value and the return value is received through reference? The following defines four global functions:

Global Functions

A & func5 (A &)
{
Return;
}

A & func6 (A)
{
Return;
}

A func7 (A &)
{
Return;
}

A func8 (A)
{
Return;
}

 

 

(5) Call func5 () in the main () function ():

Call func5 ()

Int main ()
{
A a1;
A1.x = 1;
A & a2 = func5 (a1 );
A1.x ++;
 
Cout <a1.x <endl;
Cout <a2.x <endl;

Return 0;
}

 


Func5 () output result

2
2

 

Func5 () uses a reference parameter and returns this parameter by referencing the return value. Therefore, a2 is a reference of A1.

Any changes made to a1 are reflected in a2, so the value of x is the same in a1 and a2 member variables.

(6) Call func6 () in the main () function ():

Call func6 ()

Int main ()
{
A A1;
A1.x = 1;
A & a2 = func6 (A1 );
A1.x ++;
 
Cout <a1.x <Endl;
Cout <a2.x <Endl;
 
Return 0;
}

 

A warning is reported during compilation:

Warning

Warning c00002: returning address of local variable or temporary

 


Func6 () output result

Copy
2
4198610

 

The warning means that a local variable reference is returned, which is actually incorrect. Partial Variables are returned in the function.

Therefore, an unknown memory referenced by a2 can also be seen from the output a2.x value "4198610. As for the output "Copy", it is because the value parameter is used, which has been discussed above and will not be described here.

(7) Call func7 () in the main () function ():

Call func7 ()

Int main ()
{
A a1;
A1.x = 1;
Const A & a2 = func7 (a1 );
A1.x ++;
 
Cout <a1.x <endl;
Cout <a2.x <endl;

Return 0;
}

 

Func7 () output result

Copy
2
1

 

 

This is a special usage. Because func7 () uses value return, a temporary object is generated before the function returns and a copy constructor is executed. This is equivalent to a2 referencing a temporary object. As I have said before, the temporary object will be released before the function is returned. Why is the output result normal? This is a special case, C ++ stipulates,If a temporary object has a reference, the lifetime of the temporary object will be extended to the same as that of the reference.In this way, we can explain the above output result. a2 references a temporary object instead of a1. Therefore, any a1 change will not affect a2.

Note: In the VC compiling environment, const A & a2 = func7 (a1); you can skip this line without adding "const ", however, if "const" is not added to the compiler of g ++ or other versions, a compilation error will occur. The addition of "const" is more in line with the C ++ standard because the temporary object is invisible and cannot be used to change the content of the temporary object.

(8) Call func8 () in the main () function ():

Call func8 ()

Int main ()
{
A a1;
A1.x = 1;
Const A & a2 = func8 (a1 );
A1.x ++;
 
Cout <a1.x <endl;
Cout <a2.x <endl;
 
Return 0;
}

 

Func8 () output result

Copy
Copy
2
1

 

Through the above analysis, the output result is well understood: Because the value parameter is used, it is executed in the function body.

A Copy constructor is called before, and a Copy constructor is called before the function returns. This is the origin of the first two "copies. In addition, a2 references a temporary object rather than a1, so any changes to A1.

Summary:
If reference is used to receive the reference return value, the returned reference must have a long lifetime and cannot reference local variables.
If the return value of the reference receiving value is used, a temporary object is referenced, and the lifetime of the object is extended to
.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.