Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

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

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report