Comparison of value transfer, pointer transfer, and reference transfer in C ++)

Source: Internet
Author: User

 Comparison between C ++ reference and pointer 

References are a concept in C ++. Beginners can easily confuse references with pointers.
In the following program, n is a reference of M, and M is a referent ).
Int m;
Int & n = m;
N is equivalent to M alias (nickname). Any operation on N is an operation on M.
Therefore, n is neither a copy of M nor a pointer to M. In fact, n is m itself.

Reference rules:

(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 ).

In the following example, K is initialized as a reference of I.
Statement K = J cannot be changed to a reference of J, but the value of K is changed to 6.
Since K is an I reference, the I value is also changed to 6.
Int I = 5;
Int J = 6;
Int & K = I;
K = J; // The values of K and I are changed to 6;

The main function of the reference is to pass the parameters and return values of the function.

In C ++, function parameters and return values are transmitted in three ways:
Value Transfer, pointer transfer, and reference transfer.
 

The following is a sample program for "value transfer.

Since X in the func1 function is a copy of the external variable N, changing the value of X does not affect N, so the value of N is still 0.
Void func1 (int x)
{
X = x + 10;
}
...
Int n = 0;
Func1 (N );
Cout <"n =" <n <Endl; // n = 0

The following is an example program of "pointer passing.

Since X in the func2 function is a pointer to the external variable N, changing the content of this pointer will change the value of N, so the value of N is 10.
Void func2 (int * X)
{
(* X) = (* x) + 10;
}
...
Int n = 0;
Func2 (& N );
Cout <"n =" <n <Endl; // n = 10

The following is a sample program for "reference transfer.

Since X in the func3 function is a reference of the external variable n, x and n are the same thing, changing X is equal to changing N, so the value of N is 10.
Void func3 (Int & X)
{
X = x + 10;
}
...
Int n = 0;
Func3 (N );
Cout <"n =" <n <Endl; // n = 10

Compared with the above three sample programs, we will find thatThe nature of "reference transfer" is like "pointer transfer", while the writing mode is like "value transfer ". 

In fact, "Reference" can do anything "Pointer" can also do. Why do we need to "Reference" this?
The answer is "Use appropriate tools for proper work ".

Pointers can operate anything in the memory without any constraints. Although pointers are powerful, they are very dangerous.

If you only need to borrow the "alias" of an object, use "Reference" instead of "Pointer" to avoid exceptions.

Code:

/** C ++ function parameter value transfer, reference transfer, pointer transfer */# include <iostream> using namespace STD; // 1. pass void func1 (int x) {x + = 10;} // 2. pass void func2 (Int & X) {x + = 10;} // 3. pass void func3 (int * X) {* x + = 10; // (* X) = (* x) + 10;} int main () {// 1. value Transfer int n1 = 0; func1 (N1); cout <"n1 =" <N1 <Endl; // n1 = 0 // 2. pass int n2 = 0; func2 (N2); cout <"N2 =" <N2 <Endl; // n2 = 0 // 3. pointer passed int N3 = 0; func3 (& N3); cout <"N3 =" <N3 <Endl; // N3 = 0 return 0 ;}

 

 

Related Article

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.