智能指针:
格式:auto_pre<类名>取的名字(new 类名());
名字.get() 判断智能指针是否为空,得到智能指针的裸指针
当一个智能指针向另一个智能指针赋值的时候,或者通过函数传参数的时候,就移交了管理权,再使用的该指针的时候就为空了。所以名字.release()时不调用析构函数。
eg Sample *s=sample.release();
delete s; //这样是可以的
为了解决上面的尴尬问题,引入了boost,搭建好环境,然后点击工程名,Library Search Paths 中配置环境/Users/students/Desktop/Boost/homebrew/Cellar/boost/1.57.0/lib
在Header Search Paths 中/Users/students/Desktop/Boost/homebrew/Cellar/boost/1.57.0/include
要使用boost 就要引用头文件#include<>
boost/smart_ptr.hpp>
using namespace boost;
格式:scoped_ptr<类名>取的名字(new 类名());
boost::weak_ptr<Sample>my_memory_weak;
boost::shared_ptr<Sample> my_memory(new Sample(1));
cout<<"TestaWeakPtr boost::shared_ptr UseCount:"<<my_memory.use_count()<<endl;
my_memory_weak=my_memory;
cout<<"TestaWeakPtr boost::shared_ptr UseCount:"<<my_memory.use_count()<<endl;
/*
基类中定义一个 boost::weak_ptr,用于指向子类的boost::shared_ptr,这样基类仅仅观察自己的boost::weak_ptr是否为空就知道子类有没有对自己赋值,而不影响子类的引用计数,用以降低复杂度
*/