Pointers are a powerful and effective tool inherited from C ++. There are many points where pointers are criticized. The difficulty of memory management is one of them. For example, if there are several pointers pointing to an object maintained in the heap, who is responsible for releasing the memory? It is better to say that it is difficult for a large program to clarify its logic. A poor processing may cause memory leakage.
A smart pointer is such an implementation mechanism. It uses a counter implementation principle to ensure that the last pointer to the object is responsible for releasing the memory, the management of memory is done by the pointer, reducing the workload of the program staff. The following describes how to manually implement a smart pointer:
SmartPointer. h file
[Cpp]
/*
* Author: justaipanda
* Create time: 2012/07/23 15:05:00
*/
# Ifndef _ SMARTPOINTER_H
# Define _ SMARTPOINTER_H
# Include <stdexcept>
Using namespace std;
/*
* Description:
* This template class is used as a smart pointer handler
* Which can manage the allocated memory
*/
Template <class T>
Class SmartPointer {
Public:
SmartPointer (T * p_ptr = NULL): ptr (p_ptr ),
Count (new size_t (1 )){
}
SmartPointer (const SmartPointer & sp): ptr (sp. ptr ),
Count (new size_t (sp. count )){
+ * Count;
}
SmartPointer & operator = (const SmartPointer & sp );
Bool operator = (const SmartPointer & sp) const {
Return ptr = sp. ptr;
}
T & operator * () const {
If (ptr)
Return * ptr;
Else
Throw logic_error ("unbunded pointer ");
}
T * operator-> () const {
If (ptr)
Return ptr;
Else
Throw logic_error ("unbunded pointer ");
}
~ SmartPointer (){
Destroy ();
}
Private:
T * ptr;
Size_t * count; // used to recorde the reference times
Void destroy ();
};
# Include "SmartPointer. cpp"
# Endif
SmartPointer. cpp File
[Cpp]
/*
* Author: justaipanda
* Create time: 2012/07/23 15:30:15
*/
Template <class T>
SmartPointer <T> & SmartPointer <T>: operator = (
Const SmartPointer & sp ){
If (ptr! = Sp. ptr ){
Destroy ();
Ptr = sp. ptr;
Count = sp. count;
+ * Count;
}
Return * this;
}
Template <class T>
Void SmartPointer <T >:: destroy (){
If (-- * count = 0 ){
Delete ptr;
Delete count;
}
}
Because we define a class template, the header file must contain the implementation file.
Now let's take a look at the program implementation details.
Two pointers are maintained inside the SmartPointer, one for pointing to the objects in the managed heap, and the other for referencing the counter. In the constructor, count is initialized to 1. When a copy occurs, for example, the copy constructor is called or the = Operator is used, this counter is incremented by 1. Whenever a smart pointer is assigned a new value, or the pointer itself is destroyed, it will reduce the number of references by 1 and then judge whether it is 0. If so, the object will be destroyed. This part of the code is restructured because both the value assignment overload function and the Destructor use this logic. Inside the class, a private function destroy is responsible for this part of the function.
The reference counter ensures the pointer security. Other Code ensures the availability of smart pointers, which makes it as convenient to use smart pointers as normal pointers. The primary function of a pointer is to dereference and execute operators. Therefore, this can be ensured by overloading these two operators. The logic for unreferencing is relatively simple. We mainly analyze the reload of the pointing operators:
[Cpp]
T * operator-> () const {
If (ptr)
Return ptr;
Else
Throw logic_error ("unbunded pointer ");
}
As you can see, it does not actually do any extra work, that is, it returns the maintained pointer when the pointer is bound to a specific object, it contains an important compilation feature. Let's take a look at an example of using smart pointers:
[Cpp]
/*
* Author: justaipanda
* Create time: 2012/07/23 15:38:19
*/
# Include <iostream>
# Include "SmartPointer. h"
Using namespace std;
Class {
Public:
Void test (){
Cout <"this is a test function" <endl;
}
};
Int main (){
SmartPointer <A> ptr (new ());
Ptr-> test ();
Return 0;
}
Note that the calling statement ptr-> test (); in fact, the compiler implements special processing when compiling this statement, when the compiler encounters a pointing operator, if it finds that the caller is a class that reloads the pointing operator, rather than a common pointer, it will replace the following:
[Cpp] view plaincopy
(Ptr->)-> test (); www.2cto.com
This ensures that we can use our smart pointer normally, and this mechanism is recursive. That is to say, if the pointer to the operator returns a class that reloads the pointer to the operator, instead of a normal pointer, It will be replaced recursively. Well, let's talk about that much.
Author: justaipanda