"Go" C + + references and pointers

Source: Internet
Author: User

[C + + re-learning series] references and pointers

2010-11-09 13:18 by zhenjing, 2203 reading, 10 Reviews, Favorites, compilation

Here are some common answers to the online reference and pointer differences:

There are three different types of references and pointers:
1 references must be initialized at declaration time, while pointers are not used;
2 null cannot be referenced, and the pointer can point to NULL;
3 references once declared, the referenced object cannot be changed (but the value of the object can be changed), and the pointer can change the object pointed at any time.

References can do, pointers can be, but pointers are more dangerous;

(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).

From the above differences, references are safer than pointers. From the compiler's point of view, a reference is just a more secure pointer. It is precisely because the pointer is very flexible that it is possible to make the pointer error in programming, so that pointers are the most dangerous in C + +. C + + introduces references in order to be more secure with pointer characteristics without sacrificing performance. Therefore, the difference between the two is to limit the flexibility of the pointer, such as initialization, legal address, re-assignment limit.

More explanations are given below:

1) 2) to ensure the validity of the referenced object. The reference variable declaration must be initialized, so the language restricts the validity of the reference variable as much as possible. Note: Language is only as secure as possible, and it is not fully guaranteed to be valid at the time of reference. Because the compiler can't do this. such as: a) The reference object is unintentionally destructor, often behaves as a reference to return a temporary variable, the lifetime of the reference variable is shorter than the reference variable, b) the variable used to initialize the reference variable is not constructed, mainly because of the use of pointers to initialize reference variables, such as int* p=null;  int& temp = *p; The above statement is valid, but the reference variable is not valid.

The above explanation shows that although the reference is a safe pointer, it is not completely secure. Improper use of references is also fatal. According to the C + + standard, an invalid reference produces an "indeterminate behavior." Perhaps for performance reasons, the standard does not provide a way to judge whether a reference is valid (assuming that the reference variable is valid at all times, reducing the performance penalty for judging effectiveness).

3) The value of the reference variable cannot be changed. This assumption is also to reduce the misuse of pointers. That is, you do not have to worry about the possibility of changing the actual object before and after the same reference variable when using reference variables.

When do you use the reference?

In theory, references can be used instead of pointers (references are more secure) in addition to the following two scenarios

The first is that there is no possibility to point to any object, in which case the pointer can be set to null;

The second is the need to be able to point to different objects at different times, in which case you can change the pointer's direction.

----------------------------------------------------

"Go" C + + references and pointers

Related Article

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.