c++ shared_ptr的reset(), get()

总结

1. reset(),参数为空时,会释放shared_ptr所拥有的对象,释放后执行get()会返回false

2. reset(xx),参数不为空时会先释放原来拥有的对象,再获取新对象的所有权

3. get() 判断shared_ptr当前是否拥有对象

实验如下图

 代码如下

#include <iostream>
#include <memory>
using namespace std;

class CTest 
{
public:
	CTest(int a);
	~CTest();
	void print() { cout << "value=" << val << endl; }
	void set(int a);
private:
	int val;
};

void CTest::set(int a)
{
	val = a;
}

CTest::CTest(int a) : val(a) {
	cout << "start val=" << val << endl;
}

CTest::~CTest() {
	cout << "end val=" << val << endl;
}

int main() 
{
	shared_ptr<CTest> sp;  // empty

	CTest* p1 = new CTest(10);
	sp.reset(p1);	// 从p1获取所有权
	p1->set(3);
	sp->print();

	cout << "============ 1 =============" << endl;
	CTest* p2 = new CTest(20);
	sp.reset(p2);	// 先从p1放弃所有权(造成p1回收),再从p2获取所有权
	p1->print();	// 已被回收,打印出随机值
	sp->print();

	cout << "============ 2 =============" << endl;
	sp.reset();		// 放弃p2的所有权(造成p2回收)
	p2->print();	// 已被回收,打印出随机值
	//sp->print();  // 会崩溃
	
	cout << "============ 3 =============" << endl;
	// 未获取任何对象的所有权
	if (!sp.get()) 
	{
		cout << "empty" << endl;
	}

	getchar();
	return 0;
}

  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
shared_ptrC++11中的一个智能指针类,它可以自动管理动态内存的生命周期,避免内存泄漏和悬挂指针等问题。下面是shared_ptr的代码实现,其中定义和声明分开: // shared_ptr.h template<typename T> class shared_ptr { public: // 构造函数 shared_ptr(); shared_ptr(T* ptr); shared_ptr(const shared_ptr<T>& other); // 析构函数 ~shared_ptr(); // 重载运算符 T& operator*() const; T* operator->() const; shared_ptr<T>& operator=(const shared_ptr<T>& other); // 其他成员函数 T* get() const; void reset(); void reset(T* ptr); int use_count() const; private: T* m_ptr; int* m_count; }; // shared_ptr.cpp template<typename T> shared_ptr<T>::shared_ptr() : m_ptr(nullptr), m_count(nullptr) {} template<typename T> shared_ptr<T>::shared_ptr(T* ptr) : m_ptr(ptr), m_count(new int(1)) {} template<typename T> shared_ptr<T>::shared_ptr(const shared_ptr<T>& other) : m_ptr(other.m_ptr), m_count(other.m_count) { if (m_count) { (*m_count)++; } } template<typename T> shared_ptr<T>::~shared_ptr() { if (m_count) { (*m_count)--; if (*m_count == 0) { delete m_ptr; delete m_count; } } } template<typename T> T& shared_ptr<T>::operator*() const { return *m_ptr; } template<typename T> T* shared_ptr<T>::operator->() const { return m_ptr; } template<typename T> shared_ptr<T>& shared_ptr<T>::operator=(const shared_ptr<T>& other) { if (this != &other) { if (m_count) { (*m_count)--; if (*m_count == 0) { delete m_ptr; delete m_count; } } m_ptr = other.m_ptr; m_count = other.m_count; if (m_count) { (*m_count)++; } } return *this; } template<typename T> T* shared_ptr<T>::get() const { return m_ptr; } template<typename T> void shared_ptr<T>::reset() { if (m_count) { (*m_count)--; if (*m_count == 0) { delete m_ptr; delete m_count; } m_ptr = nullptr; m_count = nullptr; } } template<typename T> void shared_ptr<T>::reset(T* ptr) { if (m_count) { (*m_count)--; if (*m_count == 0) { delete m_ptr; delete m_count; } } m_ptr = ptr; m_count = new int(1); } template<typename T> int shared_ptr<T>::use_count() const { return m_count ? *m_count : 0; }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值