将boost :: shared_ptr初始化为NULL

dom*_*lao 14 c++ boost

我可以shared_ptrNULL值初始化吗?

boost::shared_ptr<Type> s_obj(NULL);
Run Code Online (Sandbox Code Playgroud)

如果没有,那怎么样?

Yuu*_*shi 22

默认构造为您执行此操作:

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
};
Run Code Online (Sandbox Code Playgroud)


W.B*_*.B. 17

这是默认构造,即:

boost::shared_ptr<Type> s_obj;
Run Code Online (Sandbox Code Playgroud)

s_obj 现在持有一个空指针,并在经过实际测试时计算为布尔值false;