好记性不如铅笔头

C && C++, C++ Primer, 编程

C++智能指针笔记:std::shared_ptr

CONTENTS

参考链接:

http://www.cplusplus.com/reference/memory/shared_ptr/ 】
http://www.cppblog.com/Solstice/archive/2013/01/28/197597.html 】
std::shared_ptr有很多高级的用法,这里只简单的笔记下最基本的使用方式。

测试类:

class TestCls
{
public:
	string name;
	int x;
	TestCls(string _name):name(_name)
	{
		cout<<"TestCls "<<name.c_str()<<" Constructor Called\n";
	}
	~TestCls()
	 {
		 cout<<"TestCls "<<name.c_str()<<" Destructor Called\n";
	}
};

最简单的使用方法:

void testSharedPtrFunc()
{
	cout<<"enter testSharedPtrFunc\n";
	std::shared_ptr<TestCls> shptr1(new TestCls("TEST"));
	cout<<"leave testSharedPtrFunc\n";
}

输出:

enter testSharedPtrFunc
TestCls TEST Constructor Called
leave testSharedPtrFunc
TestCls TEST Destructor Called

赋值,复制等逻辑:

void testSharedPtrFunc()
{
	cout<<"enter testSharedPtrFunc\n";
	std::shared_ptr<TestCls> shptr1(new TestCls("TEST"));
	cout<<"Owner:"<<shptr1.unique()<<endl;
	cout<<"shptr1.use_count():"<<shptr1.use_count()<<endl;
	cout<<"shptr1.get():"<<shptr1.get()<<endl;

	std::shared_ptr<TestCls> shptr2(shptr1);
	cout<<"Owner:"<<shptr1.unique()<<endl;
	cout<<"shptr1.use_count():"<<shptr1.use_count()<<endl;
	cout<<"shptr2.use_count():"<<shptr2.use_count()<<endl;
	cout<<"shptr1.get():"<<shptr1.get()<<endl;
	cout<<"shptr2.get():"<<shptr2.get()<<endl;

	shptr2.reset();
	cout<<"Owner:"<<shptr1.unique()<<endl;
	cout<<"shptr1.use_count():"<<shptr1.use_count()<<endl;
	cout<<"shptr2.use_count():"<<shptr2.use_count()<<endl;
	cout<<"shptr1.get():"<<shptr1.get()<<endl;
	cout<<"shptr2.get():"<<shptr2.get()<<endl;

	std::shared_ptr<TestCls> shptr3 = shptr1;
	cout<<"Owner:"<<shptr1.unique()<<endl;
	cout<<"shptr1.use_count():"<<shptr1.use_count()<<endl;
	cout<<"shptr3.use_count():"<<shptr3.use_count()<<endl;
	cout<<"shptr1.get():"<<shptr1.get()<<endl;
	cout<<"shptr3.get():"<<shptr3.get()<<endl;

	cout<<"leave testSharedPtrFunc\n";
}

输出:

enter testSharedPtrFunc
TestCls TEST Constructor Called
Owner:1
shptr1.use_count():1
shptr1.get():0056CBE8
Owner:0
shptr1.use_count():2
shptr2.use_count():2
shptr1.get():0056CBE8
shptr2.get():0056CBE8
Owner:1
shptr1.use_count():1
shptr2.use_count():0
shptr1.get():0056CBE8
shptr2.get():00000000
Owner:0
shptr1.use_count():2
shptr3.use_count():2
shptr1.get():0056CBE8
shptr3.get():0056CBE8
leave testSharedPtrFunc
TestCls TEST Destructor Called

自定义删除函数:

这里实现一个最简单的:

void testSharedPtrFunc()
{
	cout<<"enter testSharedPtrFunc\n";
	TestCls *pTest = new TestCls("TEST");
	pTest->x = 10;
	std::shared_ptr<TestCls> shptr1(pTest,[](TestCls* p){delete p; cout<<"I am called !\n";});
	pTest = NULL;

	shptr1.reset();

	cout<<"leave testSharedPtrFunc\n";
}

输出:

enter testSharedPtrFunc
TestCls TEST Constructor Called
TestCls TEST Destructor Called
I am called !
leave testSharedPtrFunc

常用简单的函数:

void testSharedPtrFunc()
{
	cout<<"enter testSharedPtrFunc\n";
	TestCls *pTest = new TestCls("TEST");
	pTest->x = 10;
	std::shared_ptr<TestCls> shptr1(pTest);
	pTest = NULL;

	cout<<"x:"<<shptr1->x<<endl;
	cout<<"x:"<<shptr1.get()->x<<endl;

	TestCls &testCls = *shptr1;
	testCls.x = 100;
	cout<<"x:"<<testCls.x<<endl;

	shptr1.reset();

	cout<<"leave testSharedPtrFunc\n";
}

输出:

enter testSharedPtrFunc
TestCls TEST Constructor Called
x:10
x:10
x:100
TestCls TEST Destructor Called
leave testSharedPtrFunc

源码笔记:

比较多,随用随补吧。

发表评论

6 + 14 =

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据