shared_ptr<> is to weak_ptr<> as unique_ptr<> is to… what?

后端 未结 8 1111
慢半拍i
慢半拍i 2020-12-01 03:59

In C++11, you can use a shared_ptr<> to establish an ownership relation with an object or variable and weak_ptr<> to safely reference t

8条回答
  •  Happy的楠姐
    2020-12-01 04:54

    boost::optional
    

    As Billy ONeal pointed out in his answer you likely want to pass a Trebuchet& instead of a pointer. The problem with the reference is that you cannot pass a nullptr, boost::optional provides a way to have the equivilent of a nullptr. Further details on boost::optional are here: http://www.boost.org/doc/libs/1_54_0/libs/optional/doc/html/boost_optional/detailed_semantics.html

    See also this question: boost::optional vs T*

    Note: std::optional is on track to make it into C++14 but std::optional is a separate proposal that is not in the current C++14 draft. Further details here: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3672.html

提交回复
热议问题