How to use the smart pointer in Category 11
This article introduces the relevant knowledge of "how to use the intelligent pointer in Craft 11". 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!
Function prototype:
Shared_ptr& operator= (const shared_ptr& x) noexcept
Description:
Assign values to shared_ptr from existing pointers to share data management rights.
Code example:
Execution result:
From the output, you can see the ps and ps1 count, and the value of the pointer.
Note:
The declaration and assignment of the ps1 are performed in two stages, and only in this way will the assignment be performed. The following code looks like an assignment, but actually performs an initialization operation (constructor).
Mobile assignment
Function prototype:
Shared_ptr& operator= (shared_ptr&& x) noexcept
Description:
Assign values to shared_ptr from existing pointers, accompanied by the transfer of data management authority. The difference between a move assignment and a copy assignment is whether the parameter is a right value reference.
Code example:
Execution result:
From the output, you can see the ps and ps1 count, and the value of the pointer.
Note:
Although copying assignments does not cause memory leaks, in some scenarios, moving assignments can more accurately reflect the programmer's intentions and may release memory earlier.
The following code also performs a mobile assignment.
This is the end of the content of "how to use the Smart pointer in Category 11". 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!