In boost::scoped_ptr, says "It supplies a basic "resource acquisition is initialization" facility, without shared-ownership or transfer-of-ownership semantics." It is done through some non-copyable mechanism.
My question is why is there requirement for no shared-ownership?
Here is what I mean:
template <typename T>
class my_scoped_ptr
{
public:
// constructor implemented not shown here
// operator*() implemented not shown here
// operator -> implemented not shown here
~my_scoped_ptr()
{
delete my_p;
my_p = NULL;
}
private:
T * my_p;
};
void foo()
{
my_scoped_ptr<someclass> p (new someclass());
my_scoped_ptr<someclass> q (p); // both p & q point to same my_p object
// may be an exception is raised
}
Ok now , regardless whether or not exception is raised the my_p will be deleted. so, when the code get out of foo's scope ...my_scope_ptr p destructor is called, deleting my_p and setting my_p to null. Now my_scope_ptr q destructor is called again deleting my_p , which is null at this point. It seems at destruction time I could care less whether a remaining copied pointer is pointing to a valid object.
So, why would I need to care that my_scoped_ptr should not be copyable? I don't see any harm of a ptr being copyable, if the ptr takes care of deleting the object pointed to , once it exits the scope. ??!!