A smart pointer design in C ++

Source: Internet
Author: User

In C ++, smart pointers store classes that dynamically allocate objects or resources. They are mainly used to control the lifetime of resources or dynamic objects. The purpose of the design is as follows:

Ensure that dynamic objects or resources can be correctly allocated and destroyed to prevent memory leakage and other problems.
Tracks the usage of objects or resources.
The implementation of smart pointers generally uses reference counting to associate a counter with the used pointer. At this time, the reference counter tracks the external shares of the class. Therefore, there are two fundamental parts in implementation.
Count. Used to count the usage of dynamic objects or resources.
Pointer representation. It is used to indirectly express the pointer of a dynamic object or resource.
According to the smart pointer, there are two major parts above. Smart pointers can be called "smart counting Pointers". Intelligence mainly refers to the meaning of counting. Of course, the purpose of counting is different because of the application, it is only for tracking, resource management, or to prevent multiple releases.
I. smart pointer implementation
The following template class is used to encapsulate pointers. Its functions are as follows:
Pointer construction. Construct a smart pointer based on the dynamic object or resource pointer to be encapsulated. Generally, the Resource Count is added to 1 during construction;
Pointer operator overload. The *, =,-> and other operators are reloaded below.
Pointer data. A pointer to the template type, T * ptr _
[Cpp]
Template <class T>
Class scoped_refptr {
Public:
Scoped_refptr (): ptr _ (NULL ){
}
 
Scoped_refptr (T * p): ptr _ (p ){
If (ptr _)
Ptr _-> AddRef ();
}
 
Scoped_refptr (const scoped_refptr <T> & r): ptr _ (r. ptr _){
If (ptr _)
Ptr _-> AddRef ();
}
 
Template <typename U>
Scoped_refptr (const scoped_refptr <U> & r): ptr _ (r. get ()){
If (ptr _)
Ptr _-> AddRef ();
}
 
~ Scoped_refptr (){
If (ptr _)
Ptr _-> Release ();
}
 
T * get () const {return ptr _;}
Operator T * () const {return ptr _;}
T * operator-> () const {return ptr _;}
 
T * release (){
T * retVal = ptr _;
Ptr _ = NULL;
Return retVal;
}
 
Scoped_refptr <T> & operator = (T * p ){
// AddRef first so that self assignment shoshould work
If (p)
P-> AddRef ();
If (ptr _)
Ptr _-> Release ();
Ptr _ = p;
Return * this;
}
 
Scoped_refptr <T> & operator = (const scoped_refptr <T> & r ){
Return * this = r. ptr _;
}
 
Template <typename U>
Scoped_refptr <T> & operator = (const scoped_refptr <U> & r ){
Return * this = r. get ();
}
 
Void swap (T ** pp ){
T * p = ptr _;
Ptr _ = * pp;
* Pp = p;
}
 
Void swap (scoped_refptr <T> & r ){
Swap (& r. ptr _);
}
 
Protected:
T * ptr _;
};
With this class, we can define a pointer, such as a smart pointer to the class window.
[Cpp]
Scoped_refptr <window> win;
In this case, the template above will contain a pointer of window * ptr _. From the above, we can see that in order to work properly, this type of pointer must implement the AddRef and Release methods, this should not be implemented in class window, right? It does not conform to the normal logic of encapsulation. The answer is: Of course, it will not be implemented in class window. These two methods mainly aim at counting methods, and encapsulate a counter class for class window. The following counter Encapsulation

2. Counter Encapsulation
This class is very simple, there are three places to note
Class inherits from the template type, that is, from T. In the above class window, RefCountedObject is the derived class of window.
Encapsulate the counter ref_count _. Note that the addition and subtraction operations on the counter should be atomic as much as possible.
The counter class provides multiple constructor types, which support multiple constructor types of input T.
[Cpp]
Template <class T>
Class RefCountedObject: public T {
Public:
RefCountedObject (): ref_count _ (0 ){
}
 
Template <typename P>
Explicit RefCountedObject (P p): T (p), ref_count _ (0 ){
}
 
Template <typename P1, typename P2>
RefCountedObject (P1 p1, P2 p2): T (p1, p2), ref_count _ (0 ){
}
 
Template <typename P1, typename P2, typename P3>
RefCountedObject (P1 p1, P2 p2, P3 p3): T (p1, p2, p3), ref_count _ (0 ){
}
 
Template <typename P1, typename P2, typename P3, typename P4>
RefCountedObject (P1 p1, P2 p2, P3 p3, P4 p4)
: T (p1, p2, p3, p4), ref_count _ (0 ){
}
 
Template <typename P1, typename P2, typename P3, typename P4, typename P5>
RefCountedObject (P1 p1, P2 p2, P3 p3, P4 p4, P5 p5)
: T (p1, p2, p3, p4, p5), ref_count _ (0 ){
}
 
Virtual ~ RefCountedObject (){
}
 
Virtual int AddRef (){
Return Increment (& ref_count _);
}
 
Virtual int Release (){
Int count = Decrement (& ref_count _);
If (! Count ){
Delete this;
}
Return count;
}
 
Protected:
Int ref_count _;
};

Iii. Instances
[Cpp]
Scoped_refptr <Window> win (new RefCountedObject <Window> (& client, & wnd ));
The instance above defines a smart pointer win, in which the client and wnd are other parameters and are irrelevant to the definition.

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.