c++之智能指针

https://www.cnblogs.com/heleifz/p/shared-principle-application.html 

shared_ptr:https://www.cnblogs.com/heleifz/p/shared-principle-application.html

 

auto_ptr作为一个已经被摒弃得指针,有什么缺点呢,先看代码

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

int main()
{
	auto_ptr<string> a(new string("hello"));
	auto_ptr<string> b = a;
	//cout < *a << endl;   报错
	cout << *b << endl;
	return 0;
}

auto_ptr对象是对资源独占得一种智能指针,缺点在于,如果将资源转移,在使用原对象,就会发生错误

unique_ptr作为替换auto_ptr得一种指针,看如下代码

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

unique_ptr<string> demo(const char *s)
{
	unique_ptr<string> temp(new string(s));
	return temp;
}
int main()
{
	unique_ptr<string> a(new string("hello"));
	//unique_ptr<string> b = a;  //编译错误
	unique_ptr<string> b;

	b = move(a);  //标准库move可以完成移动操作
	cout << *b << endl;

	//temp是临时对象
	unique_ptr<string> c = demo("world");
	cout << *c << endl;
	
	//这里也为临时对象
	unique_ptr<string> d = unique_ptr<string>(new string("hello,world"));
	cout << *d << endl;

	return 0;
}

可以总结出,unique_ptr对auto_ptr那种直接转移资源得方法直接报错,但是有二种解决办法,一个是使用标准库move,一种是生成临时对象

shared_ptr不同于auto_ptr和unique_ptr,可以用来赋值构造和拷贝构造,这里主要说一下shared_ptr得缺点

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

class Teacher;
class Student;

class Teacher
{
public:
	Teacher()
	{
		cout << "Teacher" << endl;
	}
	~Teacher()
	{
		cout << "~Teacher" << endl;
	}
	shared_ptr<Student> Stu;
};


class Student
{
public:
	Student()
	{
		cout << "Student" << endl;
	}
	~Student()
	{
		cout << "~Student" << endl;
	}
	shared_ptr<Teacher> Tea;
};

void Test()
{
	shared_ptr<Teacher> p(new Teacher);
	shared_ptr<Student> q(new Student);
	p->Stu = q;
	q->Tea = p;
}

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

嗯,,,,详细的解释看这里  https://blog.csdn.net/henan_lujun/article/details/8984543

一个简单得shared_ptr的实现

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

template<class T>
class Shared_ptr{
public:
	Shared_ptr() :ptr(nullptr), num(nullptr){}
	Shared_ptr(T *other) :ptr(other), num(new int(1)){}
	Shared_ptr(Shared_ptr& other){
		++*other.num;
		this->num = other.num;
		this->ptr = other.ptr;
	}

	Shared_ptr& operator=(Shared_ptr& other){
		//为了防止自身赋值
		if (&other == this)
			return *this;

		//如果原来有对象
		if (num){
			if (--*num == 0){
				delete num;
				delete ptr;
			}
		}
		++*other.num;
		this->num = other.num;
		this->ptr = other.ptr;
	}

	T operator*(){
		if (ptr){
			return *ptr;
		}
		else {
			return nullptr;
		}
	}

	int getCount(){
		if (num){
			return *num;
		}
		return 0;
	}


	~Shared_ptr(){
		if (ptr){
			cout << "执行析构函数" << endl;
			if (--*num == 0){
				delete ptr;
				delete num;
				cout << "执行完毕,销毁成功" << endl;
			}
		}
	}

private:
	T *ptr;
	int *num;
};
int main(){
	Shared_ptr<string> p;
	cout << p.getCount() << endl;
	Shared_ptr<string> ptr(new string("hello,world"));
	cout << ptr.getCount() << endl;

	Shared_ptr<string> ptr1 = ptr;
	cout << ptr1.getCount() << endl;
	Shared_ptr<string> ptr2(ptr1);
	cout << ptr2.getCount() << endl;

	p = ptr;
	cout << p.getCount() << endl;

	cout << *p << endl;

	cout << *ptr << endl;
	
	cout << *ptr1<< endl;

	cout << *ptr2 << endl;



}

weak_ptr是shared_ptr的观察者,负责从shared_ptr产生一个weak_ptr但是不会增加引用计数,当shread_ptr失效以后,weak_ptr也会失效。

使用new分配内存的时候,才能使用auto_ptr和shared_ptr,使用new[]分配内存得时候,不能使用它们,只能使用unique_ptr

容器里面要使用shared_ptr,因为要保证每个对象都有一份资源

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值