Comparison Between Reference and pointer

Source: Internet
Author: User

Today, I read Lin Rui's "high quality c ++/C Programming Guide" again, and saw the comparison between references and pointers, which is more rewarding than before. So I reprinted it:

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. For example, someone named Wang xiaomao, whose nickname is "San Mao ". The reason for saying "three hairs" is actually to say three things to Wang xiaomao. Therefore, n 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 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 above program looks like playing a text game and does not reflect the value of reference. 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 for "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


Comparing the above three examples, we will find that the nature of "Reference transfer" is like "pointer transfer", and the writing method is like "value transfer ". In fact, all the things that can be done by "reference" can also be done by "pointer". Why do we need to "reference" this?

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 exceptions. For example, a person needs a proof that the seal was originally printed on the document. If he handed over the key to the seal, he would have obtained the right he did not have.
From: http://man.chinaunix.net/develop/c&c++/c/c.htm#_Toc520634026

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.