Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I noticed in your comments that you have an object allocated on the stack that you want a <code>std::shared_ptr</code> to ... if that is the case, you'll have to copy your object from the stack into the heap doing something like:</p> <pre><code>std::shared_ptr&lt;my_object_t&gt; sh_ptr(new my_object_t(stack_object)); </code></pre> <p>That will then allocate a copy of your stack-object on the heap and created a <code>std::shared_ptr</code> from that heap-allocated object. You can then copy your newly created shared pointer into a container, etc.</p> <p>Also I would concur with Nemo's comments that you should have your class <code>CurveShift</code> hold a copy of the <code>shared_ptr</code> rather than a constant reference to a <code>Curve</code> class object ... that way the <code>shared_ptr</code> object will control the life-time of the pointer reference, and should a container or some other object try to destroy the memory pointed to by the <code>shared_ptr</code> instance during it's own deconstruction, the memory reference will still be maintained in the <code>CurveShift</code> instance since calling the destructor of the <code>shared_ptr</code> elsewhere will not actually deallocate the memory for the object on the heap as your <code>CurveShift</code> instance object has an active reference to that heap allocated object held by the <code>shared_ptr</code>. By holding just a constant reference, you remove the whole advantage of using a <code>shared_ptr</code> type which is controlled deconstruction of an object allocated on the heap through some form of reference-counting. </p> <p>In the end, I would write you class like:</p> <pre><code>class CurveShift: public Curve { public: CurveShift(const shared_ptr&lt;Curve&gt;&amp; ptr): _curve(ptr) { // ... rest of constructor } private: shared_ptr&lt;Curve&gt; curve_; //will remain valid for lifetime of CurveShift instance // ... }; </code></pre> <p>And then you would make a call like:</p> <pre><code>CurveShift cs(SomeGlobalRepository::Get("key")); </code></pre> <p>Which copies in the <code>shared_ptr</code> to your <code>CurveShift</code> instance and increases the reference count to the pointer on the heap its managing.</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.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. 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