Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing boost::shared_ptr within inheritance hierarchy
    text
    copied!<p>Imagine the following situation: </p> <pre><code>class IAlarm : public boost::enable_shared_from_this&lt;IAlarm&gt; { boost::shared_ptr&lt;IAlarm&gt; getThisPointerForIAlarm() { return shared_from_this(); } void verifyThis(int); // called by Device }; class Alarm : public IAlarm { Alarm( boost::shared_ptr&lt; Device &gt; attachedDevice){ attachedDevice-&gt;attachAlarm(this-&gt;getThisPointerForIAlarm()); } void sendAlarm(){ attachedDevice-&gt;Alarm(); } }; class Device { attachAlarm( boost::shared_ptr&lt; IAlarm &gt; ia){ this-&gt;alarm=ia; } }; </code></pre> <p>I want to attach an Alarm to a Device. Alarm and Device aren't allowed to know about each other (this would end up in circular dependency). So that's why I use the Interface Class IAlarm. Finally I want to be able to attach several alarms on to one device. The alarms can access the device they are attached to and the devices can start verification on the attached Alarms</p> <p>Everything compiles nice. But if I try to attach an Alarm to a Device I get the following: </p> <pre><code>boost::shared_ptr&lt;Device&gt; ptrDevice(new Device()); boost::shared_ptr&lt;IAlarm&gt; ptrAlarm(new Alarm( ptrDevice )); terminate called after throwing an instance of 'boost::exception_detail::clone_impl&lt;boost::exception_detail::error_info_injector&lt;boost::bad_weak_ptr&gt; &gt;' what(): tr1::bad_weak_ptr </code></pre> <p>What's exactly the problem? This setup worked more or less before using <code>boost::shared_ptr</code> with references and pure pointers. Is it possible to get this work with <code>boost:shared_ptr</code>?</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