Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I assume you're using <code>boost::shared_ptr</code>... I think you want <a href="http://www.boost.org/doc/libs/1_40_0/libs/smart_ptr/shared_ptr.htm#dynamic_pointer_cast" rel="noreferrer"><code>dynamic_pointer_cast</code></a> or <code>shared_polymorphic_downcast</code>.</p> <p>These require polymorphic types, however.</p> <blockquote> <p>what kind of type <code>Base* const</code> is? <code>const Base*</code> I understand, but <code>Base* const</code>? What does <code>const</code> refer to in this case?</p> </blockquote> <ul> <li><code>const Base *</code> is a mutable pointer to a constant <code>Base</code>.</li> <li><code>Base const *</code> is a mutable pointer to a constant <code>Base</code>.</li> <li><code>Base * const</code> is a constant pointer to a mutable <code>Base</code>.</li> <li><code>Base const * const</code> is a constant pointer to a constant <code>Base</code>.</li> </ul> <p>Here's a minimal example:</p> <pre><code>struct Base { virtual ~Base() { } }; // dynamic casts require polymorphic types struct Derived : public Base { }; boost::shared_ptr&lt;Base&gt; base(new Base()); boost::shared_ptr&lt;Derived&gt; derived; derived = boost::static_pointer_cast&lt;Derived&gt;(base); derived = boost::dynamic_pointer_cast&lt;Derived&gt;(base); derived = boost::shared_polymorphic_downcast&lt;Derived&gt;(base); </code></pre> <p>I'm not sure if it was intentional that your example creates an instance of the base type and casts it, but it serves to illustrate the difference nicely.</p> <p>The <code>static_pointer_cast</code> will "just do it". This will result in undefined behavior (a <code>Derived*</code> pointing at memory allocated for and initialized by <code>Base</code>) and will likely cause a crash, or worse. The reference count on <code>base</code> will be incremented.</p> <p>The <code>dynamic_pointer_cast</code> will result in a null pointer. The reference count on <code>base</code> will be unchanged.</p> <p>The <code>shared_polymorphic_downcast</code> will have the same result as a static cast, but will trigger an assertion, rather seeming to succeed and leading to undefined behavior. The reference count on <code>base</code> will be incremented.</p> <p>See <a href="http://www.tweakbits.com/articles/sharedptr/index.html" rel="noreferrer">(dead link)</a>:</p> <blockquote> <p>Sometimes it is a little hard to decide whether to use <code>static_cast</code> or <code>dynamic_cast</code>, and you wish you could have a little bit of both worlds. It is well known that dynamic_cast has a runtime overhead, but it is safer, whereas static_cast has no overhead at all, but it may fail silently. How nice it would be if you could use <code>shared_dynamic_cast</code> in debug builds, and <code>shared_static_cast</code> in release builds. Well, such a thing is already available and is called <code>shared_polymorphic_downcast</code>.</p> </blockquote>
 

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