智能指针使用

1.unique_ptr

智能指针之unique_ptr(详解)_unique_ptr 函数参数_肥肥胖胖是太阳的博客-CSDN博客

unique_ptr 代表的是专属所有权,即由 unique_ptr 管理的内存,只能被一个对象持有。
所以,unique_ptr 不支持复制和赋值

也不建议让2个unique_ptr指向同一个对象,智能指针释放的时候,会释放2次对象,引起崩溃(会有崩溃):

int *pValue = new int(100);
unique_ptr<int> uptrValue(pValue);
unique_ptr<int> uptrValue1(pValue);
//建议 unique_ptr<int> uptrValue2(new int(100));

std::unique_ptr::release

将智能指针管理的对象释放出去,但是并不删除对象。

unique_ptr作为函数参数(

不可以使用值传递的方式进行函数参数的传递所以可以将函数定义为

void TestFunc(std::unique_ptr<TestC>& ptrC); 使用引用传递

如果定义为void TestFunc(std::unique_ptr<TestC> ptrC),则使用move来传递参数,如下 

#include <stdio.h>
#include <memory>
using namespace std;
#include <iostream>
class TestC {
public:
	TestC(int tmpa, int tmpb) :a(tmpa), b(tmpb) {
		std::cout << "construct TestC " << std::endl;
	}

	~TestC() {
		std::cout << "destruct TestC " << std::endl;
	}
	void print() {
		std::cout << "print a " << a << " b " << b << std::endl;
	}

private:
	int a = 10;
	int b = 5;
};

void TestFunc(std::unique_ptr<TestC> ptrC) {
	printf("TestFunc called \n");
	ptrC->print();
}

int main(int argc, char* argv[]) {
	std::unique_ptr<TestC> gPtrC(new TestC(2, 3));
	//unique_ptr<TestC> uptrTest = gPtrC;
	//初始化也可以写成如下这一句
	//std::unique_ptr<TestC> gPtrC = std::make_unique<TestC>(2, 3);
	TestFunc(std::move(gPtrC));
	//执行下面这一句会崩溃,因为gPtrC已经没有控制权
	gPtrC->print();
	char ch;
	cin >> ch;
	return 0;
}

2.shared_ptr

多线程环境中使用共享指针的代价非常大,为保证线程安全需要加锁,要考虑因为 share_ptr 维护引用计数而造成的上下文切换开销。使用它们是需要一定的开销的

shared_ptr::reset()全网最详细准确的解析:

调用reset的shared_ptr 对象变为empty,与对象失去关联,与该对象关联的其他shared_ptr::use_count 引用计数减一,当与对象关联的shared_ptr引用计数为0时,析构对象

class A {
public:
	A() {
		cout << "A的构造" << endl;
	}
	~A() {
		cout << "A的析构" << endl;
	}
	void slot_function() {		
		cout << "接收到了信号,并执行槽函数的逻辑  " << m_iAge << endl;
	}
	int m_iAge;
};

int main()
{	
	A *pA = new A(); 
	shared_ptr<A> pa(pA); //不推荐,推荐 pa(new A)
	int iCount = pa.use_count(); //默认引用计数为1
	shared_ptr<A> pb = pa; //pa引用计数+1, pa, pb引用计数为2
	iCount = pb.use_count();
	shared_ptr<A> pc = pb; //pa引用计数+1, pb引用计数为2
	iCount = pa.use_count(); //pa的引用为3	
	iCount = pb.use_count(); //pb的引用为3
	iCount = pc.use_count(); //pb的引用为3

	pa.reset();	
	iCount = pa.use_count(); //pa的引用为0,调用pa->slot_function();会崩溃了		
	iCount = pb.use_count(); //pb的引用为2
	iCount = pc.use_count(); //pb的引用为2	

	pc.reset();
	pb.reset(); //当A的引用计数为0的时候,会调用A的析构函数

	char ch;
	cin >> ch;	
	return 0;
}

作为参数

CDev * GetDev(shared_ptr<CDev>* sp)
{
	int iCount = sp->use_count();
	return sp->get();
}

CDev * GetDevEx(shared_ptr<CDev> sp)
{
	int iCount = sp.use_count();
	return sp.get();
}
shared_ptr<Test> sptrTest = shared_ptr<Test>(new Test());
shared_ptr<Test> sptrTest1 = sptrTest;
//shared_ptr<Test> sptrTest2 = sptrTest;
sptrTest.reset(new Test()); //原来的对象并不会立马释放,因为此时引用计数为2 -1 =1,当引用计数为1的时候释放

3. weak_ptr

它的构造不会引起指针引用计数的增加, 但是lock()返回shared_ptr对象后,会使得原来的shared_ptr引用计数加1,所以除非主动delete对象,否则对象是不会被释放的

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值