Interpretation of smart pointers (1)

一、RAII(Resource Acquisition Is Initialization)

The use of resources generally goes through three steps: obtaining resources, using resources, and destroying resources. But in practice, programmers often forget to release resources. How to make C++ automatically release resources like Java? The father of C++ gave the RAII solution. RAII translated into Chinese is "resource acquisition is initialization". It takes advantage of the automatic destruction feature of local objects in the C++ language to control the life cycle of resources, so when we use resources, they are initialized in the constructor and destroyed in the destructor.

2. Smart pointer principle

1. Basic framework

Smart pointers use RAII to automatically release resources. It is essentially a class template that can create any type of pointer object. When the smart pointer object is used up, the destructor will be automatically called to release the space pointed by the pointer.

The following is the basic framework of smart pointers:

template<class T>
class smart_ptr {
public:
	smart_ptr(T* _ptr) : ptr(_ptr) {

	}

	~smart_ptr() {
		delete ptr;
		ptr = nullptr;
	}

private:
	T* ptr;
};

2. Improvement of smart pointers

2.1

The use of smart pointers is similar to ordinary pointers. You can use the operators "*" and "->" to get the object pointed to. Therefore, we need to overload the "*" and "->" functions in the class.

template<class T>
class smart_ptr {
public:
	smart_ptr(T* _ptr) : ptr(_ptr) {

	}

	~smart_ptr() {
		delete ptr;
		ptr = nullptr;
	}

	T& operator * () {
		return *ptr;
	}

	T* operator -> () {
		return ptr;
	}
private:
	T* ptr;
};

Now, we can use smart pointers like this:

int main() {
	smart_ptr<int> p1 = smart_ptr<int>(new int(1));
	smart_ptr<string> p2 = smart_ptr<string>(new string("hello"));
	cout << *p1 << endl;
	cout << p2->c_str() << endl;

    return 0;
}

2.2

So far, our smart_ptr has not defined "copy constructor" and "assignment operator". At this time, we can only call the global "copy constructor" and "assignment operator". But unfortunately, the program will report an error if you do this!

smart_ptr<int> p1(new int(1));
smart_ptr<int> p2(p1);

Why is this?

Because both ptr1 and ptr2 point to the same resource, at the end of the program, the "resource object" will be destroyed when ptr1 is destructed, and the "resource object" will also be destroyed when ptr2 is destructed, so the resource object will be repeated. destroy. At this time, the simplest way is to disable the "copy constructor" and "assignment operator":

smart_ptr(smart_ptr&) = delete;
smart_ptr<T>& operator = (smart_ptr<T>&) = delete;

Such a simplest smart pointer is completed. I attach the complete source code below:

template<class T>
class smart_ptr {
public:
	smart_ptr(T* _ptr) : ptr(_ptr) {

	}

	smart_ptr(smart_ptr&) = delete;
	smart_ptr<T>& operator = (smart_ptr<T>&) = delete;

	~smart_ptr() {
		delete ptr;
		ptr = nullptr;
	}

	T& operator * () {
		return *ptr;
	}

	T* operator -> () {
		return ptr;
	}
private:
	T* ptr;
};

Guess you like

Origin blog.csdn.net/mars21/article/details/133012196
Recommended