best practice when returning smart pointers

前端 未结 9 1452
一生所求
一生所求 2020-12-24 11:56

What is the best practice when returning a smart pointer, for example a boost::shared_ptr? Should I by standard return the smart pointer, or the underlying raw pointer? I co

9条回答
  •  情书的邮戳
    2020-12-24 12:16

    In my opinion, in C++, you should always have to justify the use of an unguarded pointer.

    There could be many valid reasons: a need for very high performance, for very low memory usage, for dealing with legacy libraries, because of some issue with the underlying data structure the pointer is storing. But [dynamically allocated] pointers are somewhat 'evil', in that you have to deallocate the memory at every possible execution path and you will almost certainly forget one.

提交回复
热议问题