Three transfer methods for C ++ function parameters and return values: value transfer, pointer transfer, and reference transfer (emphasis)

Source: Internet
Author: User

Three transfer methods for C ++ function parameters and return values: value transfer, pointer transfer, and reference transfer (emphasis)

Comparison Between Reference and pointer

References are a concept in C ++. Beginners can easily confuse references with pointers. In the following program, n is m

A reference. 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. For example, someone named Wang xiaomao,

His nickname is "San Mao ". The reason for saying "three hairs" is actually to say three things to Wang xiaomao. So n is

It is neither a copy of m nor a pointer to m. In fact, n is m itself.

Some referenced rules are as follows:

(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 j's reference

Used to change the value of k 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 above program looks like playing a text game and does not reflect the value of reference. The main function of reference is to pass

The parameters and return values of the pass 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 for "pointer passing. Since x in the Func2 function is directed to the external variable n

Needle, changing the content of this pointer will lead to the change of n value, so the value of n becomes 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 referenced by 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

Comparing the above three examples, we will find that the nature of "Reference transfer" is like "pointer transfer", and the writing method

Such as "value transfer ". In fact, "reference" can be used to do anything "pointer". Why?

Use?

The answer is "Use appropriate tools for proper work ".

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

Like a knife, which can be used to cut trees, cut paper, manicure, and cut hair. Who dares to use it like this?

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

To avoid accidents.

 


From THISISPAN's column

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.