Smart pointers in Android: strong pointers and weak pointers

Source: Internet
Author: User

 

 

Android defines two types of smart pointers: strong pointer Sp (strong pointer) and weak pointer (weak pointer ). In fact, it is more suitable for strong references and weak references. Strong pointers share the same concept as smart pointers in general sense. The reference count is used to record how many users are using an object. If all users give up referencing this object, the object will be automatically destroyed.

 

The weak pointer also points to an object, but the weak pointer only records the address of the object and cannot access the object through the weak pointer, that is to say, the member functions of the object or the member variables of the object cannot be called by being mentally retarded. To access the objects pointed to by a weak pointer, you must first upgrade the weak pointer to a strong pointer (through the promote () method provided by the WP class ). The weak Pointer Points to an object that may be destroyed elsewhere. If the object has been destroyed, the WP promote () method returns a null pointer, in this way, address access errors can be avoided.

 

Is it amazing? How does a weak pointer achieve this? In fact, this is not complicated because every object that can be referenced by a smart pointer is appended with another weakref_impl object, this object records the strong pointer reference count and weak pointer reference count of the object. This object is used internally by the implementation of smart pointers, and users of smart pointers cannot see this object. The weak pointer operation is this object. this object will be destroyed only when both the strong reference count and the weak reference count are 0.

 

After talking about so many principles, let's see how to use smart pointers. Suppose there is a class myclass. If you want to use a smart pointer to reference the object of this class, the class must meet the following two prerequisites:

 

(1) This class is a subclass or indirect subclass of the base class refbase;

(2) This class must define a virtual constructor, that is, its constructor needs to be defined as follows:

 

Virtual ~ Myclass ();

 

A class that meets the preceding conditions can define smart pointers, which are similar to common pointers. For example, a normal pointer is defined as follows:

Myclass * p_obj;

 

The smart pointer is defined as follows:

Sp <myclass> p_obj;

 

Do not define it as sp <myclass> * p_obj. It is easy for beginners to make this mistake, which is actually equivalent to defining a pointer. Although there is no syntax problem, it is best not to use such a definition.

 

A variable that defines a smart pointer can be used as a normal pointer, including value assignment, access to object members, return values as functions, and parameters as functions. For example:

P_obj = new myclass (); // do not write p_obj = new sp <myclass> <br/> sp <myclass> p_obj2 = p_obj; <br/> p_obj-> func (); <br/> p_obj = create_obj (); <br/> some_func (p_obj); <br/> 

Do not try to delete a smart pointer, that is, delete p_obj. Do not worry about object destruction. Smart pointers are used to automatically destroy objects that are no longer in use. If you do not need to use another object, you can directly assign a null value to the pointer:

P_obj = NULL;

 

All of the above are strong pointers. The definition of weak pointers is similar to that of strong pointers, but they cannot be used to access object members. The following is an example of a weak pointer:

WP <myclass> wp_obj = new myclass (); <br/> p_obj = wp_obj.promote (); // upgrade to a strong pointer. But here we need to use. Instead of->. Actually, there is a negative pointer name. <br/> wp_obj = NULL;

 

It is very convenient to use smart pointers. Generally, it is best to use smart pointers instead of normal pointers. However, you need to know that a smart pointer is actually an object rather than a real pointer, so its running efficiency is far inferior to that of a common pointer. Therefore, we recommend that you do not use smart pointers when you are sensitive to operation efficiency.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.