Smart Pointer Interview

xiaoxiao2021-03-19  208

Smart Pointer Interview

Worm

Hello, thank you for accepting our interview. Can I simply introduce your name?

My English is called Smart Pointer, I don't know myself. Some people call me "smart pointer". Some people call me "dexterity pointer", and "smart pointers" are not very cool. However, even Smart Pointer is not a name, because I am not a pointer.

Are you not a pointer? So you are?

In fact, I am a class of classes, but my special length is the pointer to C / C . Of course, everyone will call me Pointer. So I hope everyone must remember two points: I am a class rather than pointer, but my specialty is imitation pointer.

Then how do you do like a pointer?

Is easy to accept it? C template technology and operator overloaded me a lot of space. In order to install, first, I must be highly type (Strongly Typed), template gives me this feature; secondly, I need to imitate the main two operators -> and *, then the operator is overloaded.

This seems to be very abstract, can you explain it in detail?

In fact, it is easy to sketch the basic characteristics of the Chinese: Huang's skin, black hair (except most elderly), can suffer, can save money, love play mahjong. Of course, I don't rule out the exception, I just feel that most of the Chinese I have contacted. Similarly, a probably contour can also be given for Smart Pointer.

Template class smartptr {public: smartptr (t * p = 0); SmartPtr (Const SmartPtr & P); ~ SmartPtr (); SmartPtr & Operator = (SmartPtr & P); T & Operator * () const {return * the_p; } T * Operator -> () const {return the_p;} private: t * THE_P;} As I said, this is just a probably impression, and many things can be changed. For example, you can remove or add some const, which requires depending on the specific application environment. Pay attention to the overloaded operator * and ->, it is exactly that I look like an ordinary pointer. Since I am a class, in the constructor, you can achieve some good results through the appropriate programming.

Then can you give you an example? of course can. For example, std :: auto_ptr in the C standard library is an example of a wide application. Its implementation is different in different versions of STL, but the principles are the same, probably the following look: Template class auto_ptr {public: typedef x element_type; explicit auto_ptr (x * p = 0) throw : THE_P (P) {} auto_ptr (auto_ptr & a) throw (): THE_P (A.RELEASE ()) {} auto_ptr & operator = (auto_ptr & r Hs) throw () { RHS.Release (); return * this;} ~ auto_ptr () throw () {delete the_p;} x & operator * () const throw () {return * the_p;} x * operator-> () const throw () {RETURN THE_P;} x * get () const throw () {return the_p;} x * release () throw () {x * tmp = THE_P; THE_P = 0; returnTMP;} void reset (x * p = 0) throw () {if (THE_P! = P) {delete the_p; the_p = p;}} private: x * the_p;}; About AUTO_PTR I don't want to say more, this is not our main topic today. Its main advantage is that you don't have to use Delete, you can automatically recycle spaces that have been assigned, thereby avoiding resource leaks. Many Java advocates often do not divide black and white C no garbage recycling mechanism, but it is just a laughter. Throwing a lot of commercialization and non-commercial C garbage collection libraries on the Internet do not mention that auto_ptr is enough to effectively solve this problem. And even if an abnormality is generated, Auto_PTR can also correctly recover resources. This is of great significance for the code that writes an exception-safe.

Is there anything worth noting in using the Smart Pointer? This problem is too general, there are different considerations for different Smart Pointers. For example, auto_ptr, you cannot use it in a standard container because it only keeps an instance in memory. However, I believe that two principles I have said before: Smart Pointer is a class rather than a pointer, which is an imitation pointer, then everything is good. For example, Smart Pointer as a class, then the following practices may have problems. Smartptr p; if (p == 0) if (! P) if (p) is clear, P is not a real pointer, which may be wrong. The SMARTPTR design is also very important. You can judge a BOOL SMARTPTR :: null () const. If you adhere to the above form, then you can't taste it. We will add Operator void * () Try: Template Class Smartptr {public: ... Operator void * () const {return the_p;} ... private: t * the_p;}; this It is used in Basic_ios. It can also be handled more flexibly, such as the class itself needs operation void * (), then this trick is not good. Then we have overloading Operator! () And so on. Not afraid, I am afraid I can't think of it. Can you summarize the essence of Smart Pointer? The essence of Smart Pointer is a housing, a layer of packaging. It is more than this layer of packaging, we can make things that are unable to complete, such as automatic recycling, or automatic reference counts, such as CComptr, CCOMQiptr, for example, two CComptr and CCOMQIPTR. However, everything is a double-edged sword, which is because of these functions, it will lose SMART Pointer. Keep remember that it is greatly different from the short Pointer, and the real pointer is great.

I am very accepting our interview. I wish you a good time in China! Thank you

转载请注明原文地址:https://www.9cbs.com/read-130207.html

New Post(0)