[C/C ++ syntax external function] transfers value, transfers reference, and transfers pointer
Related Concepts
C ++ Parameters |
Pass the parameter value (pass the value, or pass the value for short), transfer the parameter address (transfer the address, or transfer the address for short), and transfer the reference (referred to as transfer the reference ), corresponding function value transfer call, address transfer call, and reference transfer call |
Parameters and real parameters |
When a function is defined, parameters in the parameter table are referred to as formal parameters. When a function is called, parameters in the parameter table are referred to as actual parameters. The data transfer between real parameters and form-and-form parameters is called form-and-reality combination. |
Description |
Generally, C ++ is a value transfer call, which is unidirectional and can only be used from real parameters to form parameters. The parameter is essentially a copy of the Municipal Real parameter, so the value of the external real parameter is not changed during transmission. |
Meanings and functions of references |
That is, the alias. The function is mainly used to pass function parameters and return values. |
Referenced rule |
(1) The reference must be initialized at the same time (the pointer can be initialized at any time ). (2) there cannot be a NULL reference, and the reference must be associated with a valid storage unit (the pointer can be NULL ). (3) once the reference is initialized, the reference relationship cannot be changed (the pointer can change the object at any time ). |
|
|
Instance
# Include <iostream>
Using namespace std;
Void Swap (int x, int y );
Int main (void)
{
Int a = 1;
Int B = 2;
Cout <"a =" <a <"," <"B =" <B <endl;
Swap (a, B );
Cout <"a =" <a <"," <"B =" <B <endl;
System ("pause ");
Return 0;
}
I. Value Transfer
Void Swap (int x, int y)
{
Int temp = x;
X = y;
Y = temp;
}
Output result:
A = 1, B = 2
A = 1, B = 2
Cause: the Swap (int x, int y) function uses the value transfer method. The passed arguments are actually copies of a and B, not itself, therefore, changes to the copy will not be reflected in the and B versions.
Ii. Transfer of references
Void Swap (int & x, int & y)
{
Int temp = x;
X = y;
Y = temp;
}
Output result:
A = 1, B = 2
A = 2, B = 1
Cause: the Swap (int x, int y) function uses the reference transfer method. The passed arguments are actually references of a and B, changes to the reference will be directly reflected on a and B.
Iii. pointer Transmission
1. Change the pointer itself
Void Swap (int * x, int * y)
{
Int * temp = x;
X = y;
Y = temp;
}
Call method: Swap (& a, & B );
Output result:
A = 1, B = 2
A = 1, B = 2
Cause: the Swap (int x, int y) function uses pointer transmission. The passed arguments are actually copies of the pointers of a and B, what is changed is the copy itself rather than its indirect reference, so the pointer pointing to the value that will not be affected, that is, a and B.
2. Change the indirect reference of the pointer
Void Swap (int * x, int * y)
{
Int temp = * x;
* X = * y;
* Y = temp;
}
Call method: Swap (& a, & B );
Output result:
A = 1, B = 2
A = 2, B = 1
Cause: the Swap (int x, int y) function uses pointer transmission. Although the passed arguments are also copies of the pointers of a and B, they change the indirect references of the replicas, both the pointer and its copy point to the same value, so this change will be reflected on a and B.
Efficiency Comparison
Program code
// # Include "stdafx. h"
# Include "iostream"
# Include "windows. h"
// # Include "winbase. h"
Using namespace std;
Void TestValue (double val1, double val2 );
Void TestRef (double & val1, double & val2 );
Void TestPointer (double * val1, double * val2 );
Main ()
{
Double a = 1.5;
Double B = 5.0;
DWORD dwStartTime;
DWORD dwEndTime;
DwStartTime = GetTickCount ();
Int I;
For (I = 0; I <100000000; I ++)
TestValue (a, B );
Dwendtime = gettickcount ();
Cout <"\ ntime1 =" <dwendtime-dwstarttime <Endl;
Dwstarttime = gettickcount ();
For (I = 0; I <100000000; I ++)
Testref (A, B );
Dwendtime = gettickcount ();
Cout <"\ ntime2 =" <dwendtime-dwstarttime <Endl;
Dwstarttime = gettickcount ();
For (I = 0; I <100000000; I ++)
Testpointer (& A, & B );
Dwendtime = gettickcount ();
Cout <"\ ntime3 =" <dwendtime-dwstarttime <Endl;
Cin. Get ();
Return 0;
}
Void testvalue (double val1, double val2)
{
Double val = val1 + val2;
}
Void testref (double & val1, double & val2)
{
Double val = val1 + val2;
}
Void TestPointer (double * val1, double * val2)
{
Double val = * val1 + * val2;
}
Running result
Debug:
First: 1713 1718 1719
Second: 1714 1708 1709
Third: 1713 1718 1719
Under Release:
First: 266 266 218
Second: 282 265 219
Third: 266 265 235
You can further view the disassembly code and analyze and compare its commands.
Conclusion: Pass reference = pass pointer