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

How to use the smart pointer shared_ptr of Central11

2025-04-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article introduces the relevant knowledge of "how to use the Clear11 Smart pointer shared_ptr". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Shared_prt itself is a class, so its initialization is actually a call to the constructor of the shared_ptr class. By analyzing the constructor of shared_ptr, we can accurately grasp the initialization method of shared_ptr.

Default constructor

Function prototype:

Constexpr shared_ptr () noexcept

Description:

Declare an empty shared_ptr.

Code example:

Constructed from null pointers

Function prototype:

Constexpr shared_ptr (nullptr_t)

Description:

The shared_ptr is constructed with a null pointer, and the action default constructor acts the same.

Code example:

Constructed by pointers

Function prototype:

Explicit shared_ptr (U* p)

Description:

Construct shared_ptr based on existing pointers.

Code example:

Matters needing attention

Both of the methods shown in the example are legal, but in the second case it should be understood that once the share_ptr is constructed, it indicates that the memory management of the string object pointed to by s has been handed over to ps1 management and should no longer be accessed through s (at least very carefully).

The second way should be avoided as much as possible.

Copy construction

Function prototype:

Shared_ptr (const shared_ptr& x) noexcept

Description:

Create a new shared_ptr from an existing share_ptr. In the meantime, application counts are automatically maintained.

Code example:

Program output:

After the copy constructor is executed, the reference count increases and two pointers point to the same object.

Mobile structure

Function prototype:

Shared_ptr (shared_ptr&& x) noexcept

Description:

Create a new shared_ptr from an existing share_ptr. Shared_pt, as a parameter, also releases administrative rights to memory, and the reference count remains unchanged after the entire construction process.

Code example:

Program output:

Memory management has been transferred from ps to ps1.

Modifier description

Explicit: ensures that the constructor will not be called implicitly

Noexcept: this function does not throw an exception

Constexpr: this function can be evaluated during compilation

This is the end of the content of "how to use the Clear11 Smart pointer shared_ptr". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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: 205

*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

Internet Technology

Wechat

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

12
Report