Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can use a technique presented by <a href="http://www.boost.org/doc/libs/1_43_0/libs/smart_ptr/sp_techniques.html#preventing_delete" rel="nofollow noreferrer">Boost</a>, but my concern is that you're allowing implicit conversions from a smart pointer to a raw pointer, which is generally frowned upon on. Besides, users can call <code>delete</code> on a pointer obtained by the <code>-&gt;</code> operator, so there's really nothing you can do to prevent a determined idiot to work around whatever mechanism you come up with.</p> <p>You really should just implement a <code>get()</code> method instead of providing <code>operator T*()</code> so that at least calls to <code>delete smartptr</code> will not compile. Non-idiots should be able to figure out that there's probably a reason why that won't work.</p> <p>Yes, it's more work to type out <code>LegacyFunc(smartptr.get())</code> than <code>LegacyFunc(smartptr)</code>, but the former is preferred since it makes it explicit and prevents unexpected conversions from happening, like <code>delete smartptr</code>.</p> <p>What if you have functions like this:</p> <pre><code> void LegacyOwnPointer(SomeType* ptr); </code></pre> <p>where the function will store the pointer somewhere? This will screw up the smart pointer, because now it's not aware that something else is owning the raw pointer.</p> <p>Either way, you have some work to do. Smart pointers are like raw pointers, but they are not the same, so you can't just find-and-replace all instances of <code>T*</code> and replace it with <code>my_scoped_ptr&lt;T&gt;</code> and expect it to work just as well as before.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

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