Note that there are some explanatory texts on larger screens.

plurals
  1. POReferences to boost::smart_ptr pointed to object and checking their validity
    primarykey
    data
    text
    <p>Given the following:</p> <pre><code>class Curve { public: typedef boost::shared_ptr&lt;Curve&gt; Pointer; // ... private: // ... }; class CurveShift: public Curve { public: CurveShift(const Curve &amp; curve); // ... private: const Curve &amp; curve_; // ... }; </code></pre> <p>I was wondering whether there is a consensus best practice to use when you want to do something like:</p> <pre><code>Curve::Pointer pointer(new Curve()); SomeGlobalRepository::Add("key", pointer); </code></pre> <p>And in some other place you have:</p> <pre><code>CurveShift cs(*SomeGlobalRepository::Get("key")); </code></pre> <p>You don't really have any assurance that <code>SomeGlobalRepository</code> will contain the original <code>Curve::Pointer</code> when <code>CurveShift</code> will need it, and hence <code>curve_</code> might point to deallocated memory, or even memory that was already reallocated to something else. I believe that one possible solution would be for <code>CurveShift</code> to use a <code>boost:weak_ptr&lt;Curve&gt;</code> instead of a <code>const Curve &amp;</code>, but I am pretty certain that would mean that you can't use <code>Curve</code> on the stack anymore, and any instance would have to go on the heap (with the constructors becoming private and the objects being created through a factory). Is there a better strategy to achieve the desired behavior? My gut feeling is that while in theory this could be a serious problem, practical use cases would not lend themselves to creating these tricky situations.</p>
    singulars
    1. This table or related slice is empty.
    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.
 

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