As far as I know, it's preferred to use a smart pointer instead of managing the lifetime of a dynamically allocated object through a raw pointer,
e.g.: MyObject* obj = new Object();
But in some frameworks/libraries they always return/work with raw pointers instead of smart pointers, (maybe they have their own GC objects? I don't know).
It's also easier to work with
MyObject* obj = GetAObject(); // return raw owning pointer
than
SharedPointer<MyObject> obj = GetAObject(); // return smart pointer
Should one always use smart pointers instead of manual new
/delete
(like in the example above) or are there any cases where raw resource-owning pointers should be used?
If your code is not required to compile with a pre-C++11 compiler, I see no reason to have owning raw pointers.
Even if your code needs to compile with ancient compilers, you should check out the smart pointers from Boost.
I do not know of a single situation in which an owning raw pointer would be a good idea.
Nothing wrong with non-owning raw pointers if appropriate.
It is true that a lot of code still uses owning raw pointers. This should fall mainly into two categories: