I used to write an article about smart pointers.ArticleBut I still haven't figured out two things:
1. How can I call a member function of an object directed by a smart pointer? I don't know how to access the member method of an object through a smart pointer. Please refer to my demo of the smart pointer.
# Include <iostream> Using Namespace STD; Class A { Private : Int N; Public : ( Int M): n (m ){} ~ A () {cout <" ~ A () is called \ n " ;} Int Get () Const {Cout < " Get () is called \ n " ;}}; Class Smartptr // A's resource management class, that is, smart pointer class { Private : * A; Public : B ( * B): A (B ){} ~ B () {Delete; // Release the memory occupied by a in smartptr Cout < " ~ B () is called \ n " ;} * Get () Const {Return A ;}// The frustrated method calls its member method by returning the pointer of the managed object}; Int Main ( Void ) {Smartptr B ( New A ( 8 ); * A = B. Get (); // You do not need to write Delete (a) or delete B. Int N = A-> Get (); Cout <N < Endl; Return 0 ;}
As you can see from the above, I am frustrated by returning the pointer of A in the smart pointer class, and then accessing the method in a by pointing to. So is there any better way? Yes, just reload the-> operator. Although I don't know why the overload-> operator can be used, it does give people the feeling that smart pointers are the original pointers.CodeAs follows:
# Include <iostream> Using Namespace STD; Class Test { Private : Int N; Public : Test ( Int M): n (m ){} ~ Test () {cout < " ~ Test () is called \ n " ;} Void Get () {Cout < " Get () is called \ n " ;}}; Class Smartptr { Private : Test * N; Public : Smartptr (Test * M): n (m ){} ~ Smartptr () {cout < " ~ Smartptr is called \ n " ; Delete N; n = NULL;} Test * Operator -> ()Const// Overload-> operator. Calling methods in the test class through smartptr is like calling them directly. { Return N ;}}; Int Main (){{
Smartptr ( New Test ( 10 ); PTR -> Get ();// Calls directly through smart pointers
} Return 0 ;}
The execution result is as follows:
2. I think everyone thinks that smart pointers must be a pointer, just like int * PTR. PTR is a pointer, while int PTR is a non-pointer, actually, a smart pointer is not a pointer, just like an int PTR. Let me explain why?
First of all, I would like to talk about the principle of smart pointers. The objects pointed to by smart pointers do not need to be deleted, and we do not need to delete smart pointers because smart pointers are actually a class, classes include new and non-new objects. Suppose there is a smart pointer class smartptr. We want to create a smart pointer object like this: smartptr (new test ()), instead of smartptr * PTR = new smartptr (new test (), why? Because smart pointers are used to automatically call the Destructor after the process of the stack object, smartptr (new test () puts the PTR object in the stack, therefore, after any process ends, the smartptr destructor is automatically called, as long as the destructor in the smartptr class calls the destructor of the test class, smartptr * PTR = new smartptr (new test (), after the process ends, PTR will not automatically call the destructor of smartptr.ProgramThen, the smart pointer meaning will no longer exist.
The following is a method that uses smartptr * PTR = new smartptr (new test () to create a smart pointer object. We can compare the above Code with the graph,
# Include <iostream> Using Namespace STD; Class Test { Private : Int N; Public : Test ( Int M): n (m ){} ~ Test () {cout < " ~ Test () is called \ n " ;} Void Get () {Cout < " Get () is called \ n " ;}}; Class Smartptr { Private : Test * N; Public : Smartptr (Test * M): n (m ){} ~ Smartptr () {cout <" ~ Smartptr is called \ n " ; Delete N; n = NULL;} Test * Operator -> () Const { Return N ;}}; Int Main () {smartptr * PTR = New Smartptr (( New Test ( 10 )));// When the process exits, the Destructor will not be automatically called.} Return 0 ;}
Run the following command:
For the compiler, the smart pointer is actually a stack object, not a pointer type. When the life cycle of the stack object is approaching, the smart pointer releases the heap memory that it manages through the destructor.