在使用boost::shared_ptr的时候,有时候想将指针置为NULL。一般地,我们可以向传统指针那样,直接将NULL赋值给指针,也可以调用boos::shared_ptr的reset()方法,将指针重置,也就是设置为NULL,下面是具体的测试代码:
/*
* @,@Author: ,: your name
* @,@Date: ,: 2020-12-09 09:25:03
* @,@LastEditTime: ,: 2020-12-29 10:28:01
* @,@LastEditors: ,: Please set LastEditors
* @,@Description: ,: In User Settings Edit
* @,@FilePath: ,: /pluginManagerFramework/home/ok/test/f.cpp
*/
#include <iostream>
#include <map>
#include <boost/shared_ptr.hpp>
using namespace std;
class Test
{
int a;
public:
Test(int b)
{
setA(b);
}
void setA(int b)
{
a = b;
}
int getA()
{
return a;
}
};
int main()
{
boost::shared_ptr<Test> tptr;
tptr.reset(new Test(1));
if (tptr == NULL)
{
cout << "tptr is null" << endl;
}
else
{
cout << "tptr is not null" << endl;
}
cout << "begin to reset();" << endl;
tptr.reset();
if (tptr == NULL)
{
cout << "tptr is null" << endl;
}
else
{
cout << "tptr is not null" << endl;
}
tptr.reset(new Test(2020));
if (tptr == NULL)
{
cout << "tptr is null" << endl;
}
else
{
cout << "tptr is not null" << endl;
}
cout << "set tptr =NULL" << endl;
tptr = NULL;
if (tptr == NULL)
{
cout << "tptr is null" << endl;
}
else
{
cout << "tptr is not null" << endl;
}
return 0;
}