cpp智能指针

unique_ptr

#include <iostream>
#include <algorithm>
#include <numeric>
#include <unordered_map>
#include <string>
#include <memory>
using namespace std;
/*********************************************************/
struct Person {
	~Person() {
		cout << "delete cotr" << endl;
	}
	Person() {}
	Person(string name) { cout << "name is " << name << endl; }
};

void func1() {
	//只能由一个对象拥有这个所有权
	//pointer是栈变量,后面的是堆变量,两者生命周期绑在一起
	unique_ptr<Person>p1(new Person);
	//unique_ptr对象始终是关联的原始指针的唯一所有者。我们无法复制unique_ptr对象,它只能移动。
	//由于每个unique_ptr对象都是原始指针的唯一所有者,因此在其析构函数中它直接删除关联的指针,不需要任何参考计数。
	unique_ptr<Person>p2 = move(p1);
	if (!p1)cout << "p1 is nullptr" << endl;
	if (p2)cout << "p2 is unique" << endl;

	p2.reset(new Person);
	//输出
	//p1 is nullptr
	//p2 is unique
	//delete cotr
	//delete cotr
}
void func2() {
	auto ptr = make_unique<Person>("hj");//传入参数
	//输出
	//name is hj
	//delete cotr
}
/***********************************************************/
template <typename T>
struct Node {
	T data;
	unique_ptr<Node<T>> next;
	~Node() {
		cout << "~Node" << endl;
	}
};
template <typename T>
class Link {
public:
	//头插法(head节点始终在最前面,新插的节点在head节点之后)
	void front(const T& data) {
		auto node = make_unique<Node<T>>();
		node->data = data;
		node->next = move(head_.next);
		head_.next = move(node);
	}
	void print() {
		Node<T>*node = head_.next.get();//get返回被管理对象的指针
		while (node) {
			cout << node->data << " ";
			node = node->next.get();
		}
		cout << endl;
	}
private:
	Node<T>head_;
};
void func3() {
	Link<int>l;
	for (int val : {1, 2, 3, 4, 5}) {
		l.front(val);
	}
	l.print();
	//输出
	//	5 4 3 2 1
	//	~Node
	//	~Node
	//	~Node
	//	~Node
	//	~Node
	//	~Node

}
int main() {
	func1();
}

shared_ptr

#include <iostream>
#include <algorithm>
#include <numeric>
#include <unordered_map>
#include <string>
#include <memory>
using namespace std;

int main()
{
	// 使用 make_shared 创建空对象
	// shared_ptr<int> p1 = new int();不能这样写,不支持隐式转换(int * ==> shared_ptr<int>)
	// std::shared_ptr<int> p1(new int);这样又可以,不是隐式转换

	std::shared_ptr<int> p1 = std::make_shared<int>();
	*p1 = 78;
	std::cout << "p1 = " << *p1 << std::endl; // 输出78

	// 打印引用个数:1
	std::cout << "p1 Reference count = " << p1.use_count() << std::endl;

	// 第2个 shared_ptr 对象指向同一个指针
	std::shared_ptr<int> p2(p1);

	// 下面两个输出都是:2
	std::cout << "p2 Reference count = " << p2.use_count() << std::endl;
	std::cout << "p1 Reference count = " << p1.use_count() << std::endl;

	// 比较智能指针,p1 等于 p2
	if (p1 == p2) {
		std::cout << "p1 and p2 are pointing to same pointer\n";
	}

	std::cout << "Reset p1 " << std::endl;

	// 无参数调用reset,无关联指针,引用个数为0
	p1.reset();
	std::cout << "p1 Reference Count = " << p1.use_count() << std::endl;

	// 带参数调用reset,引用个数为1
	p1.reset(new int(11));
	std::cout << "p1  Reference Count = " << p1.use_count() << std::endl;

	// 把对象重置为NULL,引用计数为0
	p1 = nullptr;
	std::cout << "p1  Reference Count = " << p1.use_count() << std::endl;
	if (!p1) {
		std::cout << "p1 is NULL" << std::endl; // 输出
	}
	return 0;
}

给shared_ptr添加自定义删除器

#include <iostream>
#include <algorithm>
#include <numeric>
#include <unordered_map>
#include <string>
#include <memory>
using namespace std;

/*
	给shared_ptr添加自定义删除器
*/
struct Sample
{
	Sample() {
		std::cout << "Sample\n";
	}
	~Sample() {
		std::cout << "~Sample\n";
	}
};

void deleter(Sample * x)
{
	std::cout << "Custom Deleter\n";
	delete[] x;
}



int main()
{
	std::shared_ptr<Sample> p3(new Sample[3], deleter);
	return 0;

	/*

		输出:
			Sample
			Sample
			Sample
			Custom Deleter
			~Sample
			~Sample
			~Sample
	*/
}

使用Lambda 表达式 / 函数对象作为删除器

#include <iostream>
#include <algorithm>
#include <numeric>
#include <unordered_map>
#include <string>
#include <memory>
using namespace std;

/*
	使用Lambda 表达式 / 函数对象作为删除器
*/
struct Sample
{
	Sample() {
		std::cout << "Sample\n";
	}
	~Sample() {
		std::cout << "~Sample\n";
	}
};


class Deleter
{
public:
	void operator() (Sample * x) {
		std::cout << "DELETER FUNCTION CALLED\n";
		delete[] x;
	}
};

int main()
{
	// 函数对象作为删除器
	std::shared_ptr<Sample> p3(new Sample[3], Deleter());

	// Lambda表达式作为删除器
	std::shared_ptr<Sample> p4(new Sample[3], [](Sample * x) {
		std::cout << "DELETER FUNCTION CALLED\n";
		delete[] x;
		});

	/*
	
		Sample
		Sample
		Sample
		Sample
		Sample
		Sample
		DELETER FUNCTION CALLED
		~Sample
		~Sample
		~Sample
		DELETER FUNCTION CALLED
		~Sample
		~Sample
		~Sample

	*/
}

与普通指针相比,shared_ptr仅提供-> 、*和==运算符,没有+、-、++、–、[]等运算符

#include <iostream>
#include <algorithm>
#include <numeric>
#include <unordered_map>
#include <string>
#include <memory>
using namespace std;


struct Sample {
	void dummyFunction() {
		std::cout << "dummyFunction" << std::endl;
	}
};
/*
	与普通指针相比,shared_ptr仅提供-> 、*和==运算符,没有+、-、++、--、[]等运算符。
*/
int main()
{
	std::shared_ptr<Sample> ptr = std::make_shared<Sample>();

	(*ptr).dummyFunction(); // 正常
	ptr->dummyFunction(); // 正常

	// ptr[0]->dummyFunction(); // 错误方式
	// ptr++;  // 错误方式
	//ptr--;  // 错误方式

	std::shared_ptr<Sample> ptr2(ptr);
	if (ptr == ptr2) // 正常
		std::cout << "ptr and ptr2 are equal" << std::endl;
	return 0;
}

不要使用同一个原始指针构造 shared_ptr

#include <iostream>
#include <algorithm>
#include <numeric>
#include <unordered_map>
#include <string>
#include <memory>
using namespace std;

/*
	不要使用同一个原始指针构造 shared_ptr
*/

int main()
{
	int *num = new int(23);
	std::shared_ptr<int> p1(num);

	std::shared_ptr<int> p2(p1);  // 正确使用方法
	std::shared_ptr<int> p3(num); // 不推荐

	std::cout << "p1 Reference = " << p1.use_count() << std::endl; // 输出 2
	std::cout << "p2 Reference = " << p2.use_count() << std::endl; // 输出 2
	std::cout << "p3 Reference = " << p3.use_count() << std::endl; // 输出 1
	//出错
	//假如使用原始指针num创建了p1,又同样方法创建了p3,当p1超出作用域时会调用delete释放num内存,
	//此时num成了悬空指针,当p3超出作用域再次delete的时候就可能会出错。
	return 0;
}

不要用栈中的指针构造 shared_ptr 对象

#include <iostream>
#include <algorithm>
#include <numeric>
#include <unordered_map>
#include <string>
#include <memory>
using namespace std;

/*
	不要用栈中的指针构造 shared_ptr 对象
*/

int main()
{
	int x = 12;
	std::shared_ptr<int> ptr(&x);
	return 0;
	//出错:当 shared_ptr 对象超出作用域调用析构函数delete 指针&x时会出错。
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值