Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy are boost::scoped_ptr or std::unique_ptr non-copyable?
    text
    copied!<p>In <a href="http://www.boost.org/doc/libs/1_54_0/libs/smart_ptr/scoped_ptr.htm" rel="nofollow">boost::scoped_ptr</a>, 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. </p> <p>My question is why is there requirement for no shared-ownership?</p> <p>Here is what I mean:</p> <pre><code> template &lt;typename T&gt; class my_scoped_ptr { public: // constructor implemented not shown here // operator*() implemented not shown here // operator -&gt; implemented not shown here ~my_scoped_ptr() { delete my_p; my_p = NULL; } private: T * my_p; }; void foo() { my_scoped_ptr&lt;someclass&gt; p (new someclass()); my_scoped_ptr&lt;someclass&gt; q (p); // both p &amp; q point to same my_p object // may be an exception is raised } </code></pre> <p>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. </p> <p>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. ??!!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload