In Modern C++, the convention is that raw pointers are non-owning pointers - therefore code that you write and libraries that follow modern convention should not require raw pointers to be delete
d.
In order to express ownership of an heap-allocated object, smart pointers are used instead (std::unique_ptr
and std::shared_ptr
). These pointers automatically invoke delete
for you when appropriate.
Relevant core guideline:
"Never transfer ownership by a raw pointer (T*) or reference (T&)".
In your particular case:
What's not clear to me is what to do in the case of:
Ptr* p = obj.GetPtr()
Assume that p
is non-owning, as it is a raw pointer.
If you're using a legacy library, check the documentation of obj.GetPtr()
. If it needs to be delete
d, the documentation should mention it.