不多说一个Demo上来
void other(shared_ptr<vector<int>>& smart_p2)
{
shared_ptr<vector<int>>smart_p4 = smart_p2;//赋值重载
printf("1,2,3,4数组的引用计数是%d\n", smart_p2.use_count());
}
void testSharePtr()
{
shared_ptr<vector<int>> smart_p1 = make_shared<vector<int>>(10, 9);//make_shared函数得到
for (auto e : *smart_p1)
{
cout << e;
}
vector<int> * p2=new vector<int>();
*p2 = { 1,2,3,4 };
shared_ptr<vector<int>>smart_p2(p2);//使用指向堆上资源的指针构造
shared_ptr<vector<int>>smart_p3(smart_p2);//拷贝构造
other( smart_p2);
smart_p2->push_back(5);
printf("1,2,3,4数组的引用计数是%d\n", smart_p2.use_count());
cout << "数组最后一个元素变成; " << *(smart_p3->end() - 1) <<endl;
//如果2个shared_ptr指针循环调用,即A对象的指针指向B,B对象的指针指向A,则会循环引用,很好解决,把其中一个指针换成weak_ptr
}
构造方法在里面,