best practice when returning smart pointers

前端 未结 9 1395
一生所求
一生所求 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:13

    There is no "right" way. It really depends on the context.

    You can internally handle memory with a smart pointer and externally give references or raw pointers. After all, the user of your interface doesn't need to know how you manage memory internally. In a synchronous context this is safe and efficient. In an asynchronous context, there are many pitfalls.

    If you're unsure about what to do you can safely return smart pointers to your caller. The object will be deallocated when the references count reaches zero. Just make sure that you don't have a class that keeps smart pointers of objects for ever thus preventing the deallocation when needed.

    As a last note, in C++ don't overuse dynamically allocated objects. There are many cases where you don't need a pointer and can work on references and const references. That's safer and reduces the pressure on the memory allocator.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-24 12:16

    depends on your goals.

    blindly returning smart ptr to internal data might not be a good idea (which is very sensitive to the task you're trying to solve) - you might be better off just offering some doX() and doY() that use the pointer internally instead.

    on the other hand, if returning the smart ptr, you should also consider that you'll create no mutual circular references when objects end up unable to destroy each other (weak_ptr might be a better option in that case).

    otherwise, like already mentioned above, performance/legacy code/lifetime considerations should all be taken into account.

    0 讨论(0)
  • 2020-12-24 12:20

    I would never return a raw pointer, instead I would return a weak_ptr to tell the user of the pointer that he doesn't have the control over the resource.

    If you return a weak_ptr its very unlikely that there will be dangling pointers in the application.

    If there is a performance problem I would return a reference to the object and a hasValidXObject method.

    0 讨论(0)
  • 2020-12-24 12:25

    I typically return "owning"/"unique" smart pointers from factories or similar to make it clear who is responsible for cleaning up.

    This example https://ideone.com/qJnzva shows how to return a std::unique_ptr that will be deleted when the scope of the variable that the caller assigns the value to goes out of scope.

    While it's true that the smart pointer deletes its own pointer, the lifetime of the variable holding the smart pointer is 100% controlled by the caller, so the caller decides when the pointer is deleted. However, since it's a "unique" and "owning" smart pointer, no other client can control the lifetime.

    0 讨论(0)
  • 2020-12-24 12:28

    I wouldn't put raw pointers in vectors.

    In case they use auto_ptr or boost::scoped_ptr, they can't use (or return) anything but raw pointers. That could explain their way of coding, i guess.

    0 讨论(0)
提交回复
热议问题