C++ raw pointer and std::shared_ptr
No it won't. By giving the raw pointer to the shared_ptr
, you are giving shared_ptr
the responsibility of deleting it. It will do this when the last shared_ptr
object referencing your ClassA
instance no longer exists. Raw pointers don't count.
no. The shared pointer will delete it.
If you have a third party library providing you with a pointer, you need to be sure that you delete it in the correct way. If the 3rd party lib allocated it with 'malloc' for example, then you need to use the implementation of 'free' that the lib uses. You need to be sure how it was allocated.
Does the library offer a way to destroy objects it provides you with? In which case you should use that function to destroy it.
No, ClassA
object will be destroyed. Unless you didn't copied shared_ptr
somewhere out of scope so its reference counter is > 1.