Scoped_ptr has little in common with shared_ptr, weak_ptr, or unique_ptr because it is only doing very special case of "reference counting". It isn't something you will need very often in well-designed code, but it is a good tool to have available.
Basically, a scoped_ptr isn't a reference-counted thing at all. Rather, it is an object you create on the stack (within the local scope) so that you can do something like this:
//Some enclosing scope- anything set off by "{}" or even a function:
{
scoped_ptr<MyObject> ptr = new MyObject( parameters...);
} // When we hit this closing brace, "ptr" will delete the "MyObject" inside.
You tend to see this pattern more with mutexes and other synchronization primatives- I can declare an "AutoLock" that will lock the mutex passed into it, then unlock it when it deletes to turn the whole "{}" scope into a critical section.
Also notice that a 'scoped_ptr' only ever makes sense when you can't just do a plain-old stack allocation like "MyObject obj(params..)" for some reason. After all, what it is doing is letting you use a heap-allocated object as if it was one on the stack. That tends to be a lot rarer a use case than the reference-counting of shared_ptr & its cousins.
boost::scoped_ptr
. If you need transfer of ownership semantics (maintaining single ownership) then "upgrade" tostd::auto_ptr
. If you need shared ownership only then do you useboost::shared_ptr
. Also, theBoost.ptr_container
library is a nice alternative to containers of boost::shared_ptr when the elements don't really need to be shared. – Linsk