What is the difference between pointer and reference?

Source: Internet
Author: User
It is not convenient to add & or * When referencing a variable.
Defining a reference is equivalent to giving the variable an alias,
When referencing a variable, you only need to use an alias to directly reference the corresponding variable.
The pointer is not sure whether the value of its variable has been initialized.
This issue does not exist for reference. You need to consider the issue of the same name.
Basically, the two are the same, depending on what you are using
In fact, this problem is detailed in Objective C ++: In general, the reference is the same as the pointer, but according to Clause 23: when an object is returned, try not to use references, but pointers.
Convenience.
The same usage of variables and the same functionality of pointers.
Pointers do not need to be initialized,
A reference is the alias of a variable. A variable must be specified during definition.
In addition, when a reference is used as a class member, it must be specified in the member initialization table.
The referenced form participates in the fact that the parameter is the same address, and the pointer needs to allocate memory
The pointer can be non-initialized, but generally the initialization is null.
Reference is the alias of a variable and is initialized when it must be defined.
The Pointer Points to the address of the variable and uses the pointer to change the content.
The reference is a variable alias. You can also modify the variable.
Use references as much as possible, with less pointers,
The pointer can be null. The reference cannot be null.
A reference is the alias of an object.
The pointer directly refers to the memory address of the variable.
Insecure pointer
So
It can be solved by reference and pointer.
It is best to select reference
Major differences between pointers and references:
A reference is the alias of an object. It is always valid, and a pointer may be invalid.
The pointer takes up space, and the reference does not take up space.
References are a concept in C ++. Beginners can easily confuse references with pointers. In the following program, n is M's one
References, and M is a referenced object (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 neither
It is a copy of M, not 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;
}
Bytes
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
It is the same as N. Changing X is equal to changing N, so the value of N is 10.
Void func3 (Int & X)
{
X = x + 10;
}
Bytes
Int n = 0;
Func3 (N );
Cout <"n =" <n <Endl; // n = 10
Comparing the above three sample programs, we will find that the nature of "reference transfer" is like "pointer transfer", and the writing method is like
"Value transfer ". In fact, "reference" can be used to do anything "pointer". Why do we need to "reference"
This thing?
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. For example, someone needs a proof that the seal of the document should have been stamped, as shown in
If you give him the key to take the official seal, then he will have the right he shouldn't have.
----------
From "high quality c ++ programming 』
This section above is quite interesting. ^_^
Reference methods with the same variables, but with the same significance as pointers.
A pointer can be empty but cannot be referenced. The reference must be bound to the root variable.
A pointer can change the object to which it points (that is, to a different object, rather than to change its value), and a reference can only be bound with one object. Once the reference is initialized, it is the reference of this object, rather than the reference of other objects.
Although the reference seems to have the same usage method as the variable, this is also its biggest drawback. When passing a parameter, you may think that it is just a variable and that its value will not change, but the constant reference can change the value of the object. Therefore, in general, only constant reference, pointer, or variable are passed for a parameter. You should question any function parameter that is referenced in a very large amount.

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.