在使用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)

博客介绍了在使用boost::shared_ptr时,将指针置为NULL的方法。既可以像传统指针那样直接赋值NULL,也能调用其reset()方法重置指针。还提及会给出具体测试代码。

241

被折叠的 条评论
为什么被折叠?



