About pointers and references

Source: Internet
Author: User

★ Same point:
1. Is the concept of the address;
The pointer points to a piece of memory whose contents are the address of the referred memory, and the reference is the alias of a block of memory.
★ Difference:
1. The pointer is an entity, and the reference is only an individual name;
2. The reference does not need to dereference (*), the pointer needs to be dereferenced;
3. References can only be initialized once at the time of definition, and then immutable; pointers are variable;
Reference "mindedness" ^_^
4. Reference does not have a const, pointer has const,const pointer is immutable;
5. The reference cannot be null, the pointer can be empty;
6. "sizeof Reference" gets the size of the variable (object) pointed to, and the "sizeof pointer" gets the size of the pointer itself (the address of the variable or object to which it is pointing);
typeID (t) = = typeid (t&) constant is true, sizeof (t) = = sizeof (t&) is true, but when referenced as a member, it occupies the same space as the pointer (no Standard rules found).
7. The pointer and the reference self-increment (+ +) operation has different meanings;
★ Contact
1. References are implemented within the language using pointers (how do I do that?) )。
2. For general applications, the reference is interpreted as a pointer and does not make a serious semantic error. A reference is a pointer to a restricted operation (allow only the content operation).
References are concepts in C + +, and beginners tend to confuse references with pointers. In the program, N is a reference to M (reference), and M is the quoted (referent).
int m;
int &n = m;
N is equivalent to an M alias (nickname), and any action on N is an operation on M. For example, someone named Wang Xiaomao, whose nickname is "Sanmao". Say "Sanmao" how how, in fact is to Wang Xiao judge. So n is neither a copy of M nor a pointer to M, in fact N is M itself.
Some of the rules cited are as follows:
(1) The reference is created and must be initialized (the pointer can be initialized at any time).
(2) cannot have a null reference, the reference must be associated with a valid storage unit (the pointer can be null).
(3) Once a reference is initialized, the referenced relationship cannot be changed (the pointer can change the object at any time).
In the following sample program, K is initialized as a reference to I. The statement k = J cannot modify k to be a reference to J, just change the value of K to 6. Since k is a reference to I, the value of I also becomes 6.
int i = 5;
int j = 6;
int &k = i;
The values of k = J;//k and I are all changed to 6;
The above program looks like you're playing a word game and doesn't reflect the value of the quote. The primary function of a reference is the parameter and return value of the passed function. In the C + + language, the parameters and return values of a function are passed in three ways: value passing, pointer passing, and reference passing.
The following is a sample program for "Value passing". Since x in the FUNC1 function body 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 a sample program for "pointer passing". Since x in the FUNC2 function body is a pointer to an external variable n, changing the contents of the pointer will cause the value of N to change, so the value of n becomes 10.
void Func2 (int *x)
{
(* x) = (* x) + 10;
}
& #8943;
int n = 0;
FUNC2 (&n);
cout << "n =" << n << Endl; n = 10
The following is a sample program for "reference passing." Since x in the FUNC3 function body is a reference to an external variable n, x and n are the same thing, changing x equals changing n, so the value of n becomes 10.
void Func3 (int &x)
{
x = x + 10;
}
& #8943;
int n = 0;
FUNC3 (n);
cout << "n =" << n << Endl; n = 10
In contrast to the above three sample programs, it is found that "reference passing" is of the nature of "pointer passing" and is written like "value passing". In fact, anything that "references" can do "pointers" can also be done, why "reference"
This thing?
The answer is "do the right thing with the proper tools."
Pointers can manipulate things in memory without restraint, although pointers are powerful, but very dangerous.
Like a knife, it can be used to cut trees, cut paper, manicure, barber, etc., who dares to use it?
If you really only need to borrow an "alias" for an object, use "reference" instead of "pointer" to avoid an unexpected occurrence. For example, someone needs a proof that the seal on the document would have been stamped on it, and if the key to the official seal is given to him, then he has acquired the right not to.

About pointers and references

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.