pointers are used for each programming language. C + + exposes pointers to users (programmers), while languages such as Java and C # hide pointers. "Everything uses pointers. C + + just exposes them rather than hiding them, "It ' s easier to give someone an address to your home than to give a copy of Your home to everyone.② the advantages and necessity of using pointers on the other hand:What is the difference between a reference and a pointer? Nature: References are aliases, pointers are addresses, specific:
- Pointers can effectively represent the data structure;
- Can dynamically allocate memory, realize the free management of memory;
- The use of strings can be more convenient;
- Easy and efficient use of arrays
- The pointer is directly related to the storage address of the data, such as: value delivery is not as efficient as address delivery, because the value is passed from the address of the actual parameter, and then the parameter is assigned to the function calculation, and the pointer takes the address of the formal parameter directly to the address of the argument, the use of direct data extraction, efficiency, Especially in the case of frequent assignments (note: Changes in parameters can affect the value of an argument!) ）
① from a phenomenon, the pointer can change the value it points to when it is run, and the reference will not change once it is bound to an object. This sentence can be understood as: The pointer can be re-assigned to point to another different object. But the reference always points to the object that was specified at initialization and cannot be changed later, but the contents of the specified object can be changed.
② from memory allocation, the program allocates memory regions for pointer variables instead of allocating memory regions for references, because the reference declaration must be initialized to point to an already existing object. The reference cannot point to a null value.
③ from compilation, the program adds pointers and references to the symbol Table, the variable name and the corresponding address of the variable are recorded on the symbol table. The corresponding address value of the pointer variable on the symbol table is the address value of the pointer variable, and ReferencesThe corresponding address value on the symbol table is address value of the Reference object。 The symbol table does not change after it is generated, so the pointer can change the object that is pointed to (the value in the pointer variable can be changed), and the reference object cannot be changed. This is the primary reason for using referential security using pointers that are unsafe. In a way, in a sense, a reference can be considered a pointer that cannot be changed。Note: The standard does not specify the reference to use memory, there is no provision for the specific reference to how to implement, specifically with the compiler http://bbs.csdn.net/topics/320095541
The fact that ④ does not have a reference to a null value means that using the referenced code is more efficient than using pointers. Because you do not need to test its legitimacy before using a reference. Instead, the pointer should always be tested to prevent it from being empty.
⑤ Theoretically, there is no limit to the progression of pointers, but the reference can only be one level. As follows:
int** P1; Legal. Pointer to pointer
int*& P2; Legal. Reference to Pointer
int&* P3; Illegal. Pointers to references are illegal
int&& P4; Illegal. References to references are illegal
Note that the above reading method is from left to right。
The following is an overview of what you can say in plain words:
Note: Because the pointer can not point to any object, you must do a null operation before using the pointer, and the reference does not have to.
For example, in the case of the + + operation, the action on the reference directly responds to the object pointed to, not to the point, whereas the manipulation of the pointer causes the pointer to point to the next object instead of changing the contents of the object being referred to.
In particular, the constWhy would you mention the Const keyword? Because Const has a difference in the qualification of pointers and references: constant pointer vs constant reference★ constant Pointer: A pointer to a constant that is const before the type of the pointer definition statement, indicating that the object being pointed to is a constant.A pointer to a constant restricts only the indirect access operation of the pointer, not the operation of the value itself pointed to by the pointer.
And there is no so-called constant reference constant, because reference variables are reference constants。 C + + does not differentiate between a const reference to a variable and a reference to a const variable. A program should never reassign a reference to another variable, so the reference is always const. If you apply the keyword const to a reference, the effect is to make its target a const variable. Thatdefining "Const int* const POINTER=&C" tells the compiler that pointer and *pointer are constants, and they cannot operate as lvalue values.
No: const double const& a=1; only const double& a=1;Summary: There is a rule that distinguishes whether Const is a modifier pointer, or the data that the pointer points to-draw a vertical asterisk (*) through the pointer declaration, if const appears online left， the data pointed to by the pointer is a constant; If the const appears in the Right， The pointer itself is a constant。 The reference itself is a constant, that is, the point cannot be changed.
const double& a=b;
a=3;//出错error: assignment of read-only reference `a‘
To better understand pointers and references, the following describes pointer passing and reference passing. How do pointers and references function as arguments to pass values?
Reference passing and pointer passing are different, although they are a local variable on the function stack space, but any handling of reference parameters is handled by an indirection to the relevant variables in the key function. For pointers passing parameters, if you change the address of the pointer in the called function, it will not affect the relevant variable of the key function. If you want to change the related variable in the key function by passing the pointer parameter, you have to use a pointer to the pointer, or a pointer reference .
Conceptually speaking. A pointer is essentially a variable that holds the address of a variable, logically independent, and can be changed, including the change in the address it points to and the data stored in the address it points to.
Whereas a reference is an alias, it is logically not independent, its existence is dependent , so the reference must be initialized at the outset, and the object it refers to cannot be changed throughout its life cycle (it can only be attached to the same variable from beginning to end).
Finally, summarize the same points and different points of pointers and references:
★ Same Point :
Are the concept of addresses;
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.
★ different points :
The pointer is an entity, and the reference is only an individual name;
References can only be initialized once at the time of definition, immutable, pointers variable, reference "mindedness", pointers "inconstant";
The reference does not have a const, the pointer is const, and theconst pointer is immutable;
Specifically, there is no int& const A in this form, and the const int& A is, the former guideline with itself that alias can not be changed, which is of course, so do not need this form, the latter refers to the value of the reference can not be changed )
The reference cannot be null, the pointer can be empty;
The "sizeof Reference" gets the size of the variable ( object ) pointed to, and the "sizeof pointer" Gets the size of the pointer itself;
Pointer and reference self-increment (+ +) operation has different meanings;
References are type-safe, while pointers are not ( references are more type-checked than pointers)
Talking about the relation and difference between the reference and the pointer