Get the App
SLTechnology News&Howtos  ›  Development  › 

What is the difference between the reference and pointer of C++

Shulou Source: shulou.com Published: 2022-06-02 09:43:28 09月12日 Update

This article mainly explains "what is the difference between the citation and pointer of C++". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "what is the difference between the citation and pointer of C++?"

There are three differences between references and pointers:

1 references must be initialized at declaration time, and pointers do not use

2 cannot be referenced for NULL, but the pointer can point to NULL

3 once a reference is declared, the referenced object cannot be changed (but the value of the object can be changed), and the pointer can change the object to which it points at any time.

References can do it, so can pointers, but pointers are more dangerous.

(1) the reference must be initialized at the same time it is created (the pointer can be initialized at any time).

(2) there cannot be a NULL reference, which must be associated with a legitimate storage unit (the pointer can be NULL).

(3) once the reference is initialized, the relationship of the reference cannot be changed (the pointer can change the object at any time).

Judging 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 pointers are so flexible that they are more likely to go wrong when programming, so to speak, pointers are the most dangerous in C++. In order to make more secure use of pointer features without sacrificing performance, C++ introduces references.

As can be seen from the above differences, the differences between the two are all reflected in the restrictions on a flexible feature of the pointer, such as initialization, legal address, and re-assignment.

More explanations are given here one by one:

1) to ensure the validity of the referenced object.

The reference variable declaration must be initialized, but the language limits the validity of the reference variable as much as possible. But note: the language is only guaranteed as much as possible, not the validity of the reference at all times. Because the compiler can't do that.

Such as:

A) the reference object is unintentionally destructed; it is often represented as a reference that returns a temporary variable; the lifetime of a reference variable is shorter than that of a reference variable

B) the variable used to initialize the reference variable is not constructed, mainly because the reference variable is initialized with a pointer

Such as:

Int* paired null; int& temp = * p

The above statement is valid, but the reference variable is invalid.

The above explanation shows that although references are safe pointers, they are not absolutely safe. Misuse of quotation can also be fatal. According to the C++ standard, an invalid reference will produce "uncertain behavior". Perhaps for performance reasons, the standard does not provide a way to determine the validity of the reference (according to the assumption that the reference variable is always valid, reducing the performance loss of judging validity).

2) the value of the reference variable cannot be changed.

This assumption is also to reduce the misuse of pointers. In other words, when you use a reference variable, you don't have to worry about the possibility that the actual object pointed to before and after the same reference variable may change.

So when do you use references?

In theory, references can be used instead of pointers except in the following two cases (references are more secure)

One is that you consider the possibility of not pointing to any object, in which case you can set the pointer to null.

Second, you need to be able to point to different objects at different times, in which case you can change the direction of the pointer.

* give you an example to help you understand.

Class Time// defines a class Time {public:// data member is a public int hour; int minute;} Time tprecincter cout / defines an object of a class t, and a pointer variable of class p, called reference, also refers to the address where the pointer p points to t

Tags: Pointers variables objects pointing valid cased security situation faceted performance time moment validity limitation different legal dangerous content address actual Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology Apple Shulou Information MariaDB Redmi