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 realize auto_ptr

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

Share

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

This article mainly shows you "how to achieve auto_ptr", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "how to achieve auto_ptr" this article.

There are two ways to implement auto_ptr:

The first method: I have implemented it in the last blog, the main idea is the transfer of management power.

The second method: it is a previous version of our C++ standard library, the main idea is that in the auto_ptr class, in addition to a member variable with a pointer, there is also a member variable of type bool _ owner. Set _ owner to true in the constructor, indicating that the object is the owner of the memory pointed to by the pointer. When you want to assign a value (ap1=ap2), set the _ owner of ap1 to the _ owner of true,ap2 and false.

So when we destruct an object, we just check to see if its _ owner is true. Free memory if it is true, and not if it is not true.

The main implementation is as follows:

# includeusing namespace std;templateclass AutoPtr {public: AutoPtr (T* ptr): _ ptr (ptr), _ owner (true) {} ~ AutoPtr () {if (_ owner) {delete _ ptr }} AutoPtr (AutoPtr& ap): _ ptr (ap._ptr), _ owner (true) {ap._owner = false;} AutoPtr& operator= (AutoPtr& ap) {if (this! = & ap) {if (_ owner) {delete _ ptr } _ ptr = ap._ptr; _ owner = true; ap._owner = false;} return * this;} T* operator- > () {return _ ptr;} T & operator* () {return * _ ptr;} private: T* _ ptr; bool _ owner }

After reading the implementation of auto_ptr, some people will ask:

It seems that this method is better, it can achieve the use of general pointers, there can be multiple pointers to the same memory, and all can access this memory, and we know that the biggest disadvantage of the new version of auto_ptr implementation (management transfer) is that there can not be several pointers to the same memory, a smart pointer can only point to one memory.

Since the old version of the smart pointer implementation is easier to use than the new version, why is it replaced?

Take a look at the following code:

AutoPtr ap1 (new int (1)); if (1) {AutoPtr ap2 (ap1);} * ap1 = 3

This code uses the old version of smart pointer (ap1) to point to a dynamically opened memory, and then in the if conditional statement there is another ap2 pointing to this memory. We will know that according to the implementation principle of the old version of smart pointer, the _ owner of ap1 is false,ap2 and the _ owner is true. In addition to the local scope of the if conditional statement, ap2 automatically calls the destructor to release memory, so when we access a piece of memory that has been released when we are outside * ap1=3, then the program will have a problem.

If it is a new version of auto_ptr, it provides a public member function GetPtr (), which can get the pointer _ ptr, and when this happens, it can determine whether _ ptr is empty before accessing memory. It is useless in the old version to do so because the _ ptr of ap1 is not empty.

The above is all the contents of the article "how to achieve auto_ptr". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

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