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's the difference between pointers and references in C++?

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces the relevant knowledge of "what's the difference between pointers and citations in C++". The editor shows you the operation process through an actual case. The method of operation is simple, fast and practical. I hope this article "what's the difference between pointers and citations in C++" can help you solve the problem.

The pointer in C + + is a variable that holds the memory address of another variable. A reference is an alias for a variable that already exists. Once a reference is initialized to a variable, it cannot be changed to reference another variable. Therefore, references are similar to const pointers (don't be confused with pointers to constant values! ).

Main distinguishing pointer

The pointer can be initialized to any value at any time after it is declared.

Int a = 5 position / some codeint * p = & a

You can assign a pointer to a null value.

The pointer needs to use *.

The pointer can instead point to any variable of the same type.

Example:

Int a = 5 ntint * ptert p = & a terint b = 6 nit p = & b; reference

References must be initialized when they are declared.

Int a = 5int & ref = a

Reference cannot be NULL.

You can simply use references by name.

Once a reference is initialized to a variable, it cannot be changed to reference a variable object.

Other differential memory details

The pointer has its own memory address and size on the stack, while the reference shares the same memory address (as the original variable), but also takes up some space on the stack.

Arithmetic operation

Various arithmetic operations can be performed on pointers without something called referential arithmetic. (but you can take the address of the object that a reference points to and pointer it as you did in & obj + 6).

When are pointers and references used

The performance is exactly the same because references are internally implemented as pointers. But you can still remember a few points to decide when and what to use:

Use reference:

In function arguments and return types. Use the pointer:

If you need pointer operations or pass NULL pointers, use pointers. For example, for arrays (note that array access is achieved using pointer arithmetic).

To implement linked lists, trees and other data structures and their algorithms, because we point to different cells, we must use the concept of pointers.

As stated in C++ 's official FAQ:

You can use references when you can and pointers if necessary.

When you do not need to "reset", references usually take precedence over pointers. This usually means that references are most useful in the public interface of the class. References are usually used on the appearance of objects, and pointers are used internally.

The exception above is that the parameter or return value of a function requires a "sentinel" reference-a reference that does not reference an object. This is usually done by returning / getting the pointer and giving the nullptra value a special meaning (references must always be aliased objects, not dereferenced null pointers).

Note: old C programmers sometimes don't like references because the reference semantics they provide are not clear in the caller's code. However, after some C++ experience, people soon realized that this is a form of information hiding, it is an asset rather than a responsibility. For example, programmers should write code in the language of the problem rather than the language of the machine.

This is the end of the introduction on "what's the difference between pointers and references in C++". Thank you for your reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

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