returning a 'pointer' which is required to be held by a smart pointer

后端 未结 3 567
借酒劲吻你
借酒劲吻你 2021-02-02 02:37

I have a project which I would like to make more use of smart pointers. Overall, I have been successful in this goal. However, I\'ve come across one things which I\'m not sure w

3条回答
  •  北荒
    北荒 (楼主)
    2021-02-02 03:12

    If you build a factory that's ok that you simply return a pointer. And the user of your factory can make his own decision how to and where to put this pointer.
    If you need to enforce to use smart pointer you have to restrict choice as you don't want them to use "wrong" ones.
    So boost::shared_ptr. But better to typedef it then to MyClassPtr or MyClass::ptr.
    Still, factories they are like "new". When I want I put result of new inside of std::auto_ptr. But I don't want to be forced to call "release" all the times when I don't want smart pointer.

提交回复
热议问题