C++ 将 boost::shared_ptr 初始化为 NULL
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16229401/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 20:09:34 来源:igfitidea点击:
Initialize a boost::shared_ptr to NULL
提问by domlao
Can I initialize shared_ptr
with NULL
value?
我可以shared_ptr
用NULL
值初始化吗?
boost::shared_ptr<Type> s_obj(NULL);
If not, then how?
如果不是,那怎么办?
回答by Yuushi
The default construction does this for you:
默认构造为您执行此操作:
template<class T> class shared_ptr
{
public:
explicit shared_ptr(T * p = 0): px(p)
{
//Snip
}
//...
private:
T * px; // contained pointer
count_type * pn; // ptr to reference counter
};
回答by W.B.
That's the default construction, i.e.:
这是默认构造,即:
boost::shared_ptr<Type> s_obj;
s_obj
now holds a null pointer and evaluates to boolean false when truth-tested;
s_obj
现在持有一个空指针,并在真值测试时评估为布尔假;