Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use the smart pointer shared_ptr of Central11

Shulou Source: shulou.com Published: 2022-06-01 19:20:45 10月14日 Update

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!

Tags: Function pointer code prototype instance example management output intelligence memory content action actual object situation copy point way more knowledge Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno Redmi vpn MySQL Linux