C++ 智能指针 unique ptr

unique_ptr 提供了一种独占所有权的智能指针,允许动态分配的对象在其生命周期结束时自动释放内存。这种特性在 C++11 中是非常有用的,因为它有助于避免内存泄漏和悬挂指针等常见问题,同时也提高了代码的安全性和可靠性。

我们重点从以下几个方面来学习 unique_ptr :

  1. unique_ptr 使用
  2. unique_ptr 特性
  3. unique_ptr 自定义删除器

1. unique_ptr 使用

我们可以使用 unique_ptr 类的构造函数创建智能指针对象,来管理由 new、new[] 创建出的动态对象。

#if 1
#include <iostream>
using namespace std;


class Person
{
public:
    Person()
    {
        cout << "构造函数" << endl;
    }

    void Demo()
    {
        cout << "Person Demo" << endl;
    }


    ~Person()
    {
        cout << "析构函数" << endl;
    }
};

// 1. unique_ptr 创建方式
void test01()
{
    // 1.1 管理单一动态对象
    unique_ptr<Person> up1(new Person);
    up1->Demo();

    // 1.2 管理动态对象数组
    unique_ptr<Person[]> up2(new Person[2]);
    up2[0].Demo();
    up2[1].Demo();
}

// 2. unique_ptr 操作函数
void test02()
{
    unique_ptr<Person> up(new Person);

    // 2.1 get 成员函数返回 unique_ptr 管理的动态对象指针
    Person* person1 = up.get();

    // 2.2 release 成员函数使 unique_ptr 不再持有动态对象指针(并不销毁管理的对象),并返回其管理的动态指针。
    Person* person2 = up.release();
    delete person2;

    // 2.3 reset 成员函数有两个重载的版本,具体功能如下:
    up.reset();  // 释放并销毁 unique_ptr 所管理的动态对象指针。
    up.reset(new Person);  // 释放并销毁原来的管理的动态对象,并重新持有新创建的动态对象

    // 2.4 swap 成员函数交换两个 unique_ptr 对象管理的动态对象
    unique_ptr<Person> sp(new Person);
    up.swap(sp);
}

int main()
{
    // test01();
    test02();
    return 0;
}


#endif

2. unique_ptr 特性

unique_ptr 特点:

  1. 同时只能有一个 unique_ptr 对象来持有动态对象资源
  2. unique_ptr 对象不支持默认拷贝、默认赋值语义
  3. unique_ptr 对象支持移动拷贝、移动赋值语义

由此,我们知道,unique_ptr 对象不能以值的方式用做函数参数,也不能存储到STL 的容器中(容器要求元素必须能够被拷贝)。

#if 0
#include <iostream>
#include <memory>
#include <vector>
using namespace std;

class Person
{
public:
	Person()
	{
		cout << "构造函数" << endl;
	}

	~Person()
	{
		cout << "析构函数" << endl;
	}
};

void test()
{
	unique_ptr<Person> up1(new Person);
	unique_ptr<Person> up2(new Person);

	// 1. 禁止拷贝、赋值
	// unique_ptr(const unique_ptr&)            = delete;
	// unique_ptr& operator=(const unique_ptr&) = delete;

	// 2. 允许移动拷贝、赋值
	unique_ptr<Person> up3(move(up1));  // 移动拷贝
	up2 = move(up3);  // 移动赋值

	// 由此, unique_ptr 不允许作为容器元素
	// vector<unique_ptr<Person>> vec;
	// vec.push_back(up1);
}

int main()
{
	test();
	return 0;
}

#endif

3. unique_ptr 自定义删除器

unique_ptr 可用于管理 new 出来的动态对象,也可以管理其他需要手动关闭的资源。例如:文件对象。

由于 unique_ptr 默认使用 delete、delete[] 来释放被管理的资源。所以,当管理的对象不能通过 delete、delete[] 来释放时,就需要自定义删除器。

#if 1
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <functional>
using namespace std;

class Person
{
public:
	Person()
	{
		cout << "构造函数" << endl;
	}

	~Person()
	{
		cout << "析构函数" << endl;
	}
};

struct Deleter
{
	void operator()(FILE* fp)
	{
		cout << "文件被自动关闭" << endl;

		if (fp != nullptr)
		{
			fclose(fp);
			fp = nullptr;
		}
	}
};

void my_deleter(FILE* fp)
{
	cout << "文件被自动关闭" << endl;

	if (fp != nullptr)
	{
		fclose(fp);
		fp = nullptr;
	}
}


void test()
{
	// 1. 函数对象作为删除器
	// unique_ptr<FILE, Deleter> up(fopen("./demo.txt", "w"), Deleter());
	// unique_ptr<FILE, function<void(FILE*)>> up(fopen("./demo.txt", "w"), Deleter());
	
	// 2. 普通函数作为删除器
	// unique_ptr<FILE, decltype(&my_deleter)> up(fopen("./demo.txt", "w"), my_deleter);
	// unique_ptr<FILE, void(*)(FILE *)> up(fopen("./demo.txt", "w"), my_deleter);
	// unique_ptr<FILE, function<void(FILE*)>> up(fopen("./demo.txt", "w"), my_deleter);
	
	// 3. 匿名函数作为删除器
	unique_ptr<FILE, function<void(FILE *)>> up(fopen("./demo.txt", "w"), [](FILE *fp) {
			cout << "文件被自动关闭" << endl;

			if (fp != nullptr)
			{
				fclose(fp);
				fp = nullptr;
			}
		});


	if (!up)
	{
		cout << "文件打开失败" << endl;
		return;
	}
	fputs("hello world\n", up.get());
}

int main()
{
	test();
	return 0;
}

#endif

未经允许不得转载:一亩三分地 » C++ 智能指针 unique ptr
评论 (0)

4 + 2 =