<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[[boost::shared_ptr] *** downcast, wie?]]></title><description><![CDATA[<p>Hallo, folgendes Problem:</p>
<pre><code>struct Basis {virtual ~Basis(){}  };
struct Derived : public Basis {};

int main()
{
    Basis* ptr = new Derived();
    Derived* ptrDerived = dynamic_cast&lt;Derived*&gt;(ptr);
}
</code></pre>
<p>so und wie funzt das analog mit:</p>
<pre><code>boost::shared_ptr&lt;Basis&gt; ptr(new Derived);
   boost::shared_ptr&lt;Derived&gt; ptrDerived = dynamic_cast&lt;Derived*&gt;(ptr); //&lt;-funzt natürlich nicht!  :rolleyes:
</code></pre>
<p>gruß, SirAnn</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/173362/boost-shared_ptr-downcast-wie</link><generator>RSS for Node</generator><lastBuildDate>Fri, 18 Sep 2026 07:40:04 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/173362.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 14 Feb 2007 14:33:05 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to [boost::shared_ptr] *** downcast, wie? on Wed, 14 Feb 2007 14:33:05 GMT]]></title><description><![CDATA[<p>Hallo, folgendes Problem:</p>
<pre><code>struct Basis {virtual ~Basis(){}  };
struct Derived : public Basis {};

int main()
{
    Basis* ptr = new Derived();
    Derived* ptrDerived = dynamic_cast&lt;Derived*&gt;(ptr);
}
</code></pre>
<p>so und wie funzt das analog mit:</p>
<pre><code>boost::shared_ptr&lt;Basis&gt; ptr(new Derived);
   boost::shared_ptr&lt;Derived&gt; ptrDerived = dynamic_cast&lt;Derived*&gt;(ptr); //&lt;-funzt natürlich nicht!  :rolleyes:
</code></pre>
<p>gruß, SirAnn</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1228806</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1228806</guid><dc:creator><![CDATA[muffmolch]]></dc:creator><pubDate>Wed, 14 Feb 2007 14:33:05 GMT</pubDate></item><item><title><![CDATA[Reply to [boost::shared_ptr] *** downcast, wie? on Wed, 14 Feb 2007 15:02:49 GMT]]></title><description><![CDATA[<p>Gibt es keine Methode, die den Pointer zurückliefert? Gleich mal gucken.</p>
<p>Edit: Entweder shared_ptr::get() oder get_pointer(shared_ptr&amp;)</p>
<p>Aber für dich das richtige ist wohl eher:</p>
<p><strong>dynamic_pointer_cast</strong></p>
<p>template&lt;class T, class U&gt;<br />
shared_ptr&lt;T&gt; dynamic_pointer_cast(shared_ptr&lt;U&gt; const &amp; r);<br />
Requires: The expression dynamic_cast&lt;T*&gt;(r.get()) must be well-formed and its behavior defined.</p>
<p>Returns:</p>
<p>When dynamic_cast&lt;T*&gt;(r.get()) returns a nonzero value, a shared_ptr&lt;T&gt; object that stores a copy of it and shares ownership with r;<br />
Otherwise, an empty shared_ptr&lt;T&gt; object.<br />
Throws: nothing.</p>
<p>Notes: the seemingly equivalent expression</p>
<p>shared_ptr&lt;T&gt;(dynamic_cast&lt;T*&gt;(r.get()))</p>
<p>will eventually result in undefined behavior, attempting to delete the same object twice.</p>
<p>Von <a href="http://www.boost.org/libs/smart_ptr/shared_ptr.htm#Members" rel="nofollow">http://www.boost.org/libs/smart_ptr/shared_ptr.htm#Members</a></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1228823</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1228823</guid><dc:creator><![CDATA[viande]]></dc:creator><pubDate>Wed, 14 Feb 2007 15:02:49 GMT</pubDate></item><item><title><![CDATA[Reply to [boost::shared_ptr] *** downcast, wie? on Wed, 14 Feb 2007 17:29:09 GMT]]></title><description><![CDATA[<p>ahh, ich blinder vollhonk... bin etwas überarbeitet und habe gleich google bemüht ohne in der references zu schauen. ein noob-fehler... aber was soll's.</p>
<p>1000 dank. das haut natürlich hin...</p>
<p>aber der vollständigkeit halber:</p>
<p>so geht's:</p>
<pre><code>boost::shared_ptr&lt;Derived&gt; ptrDerived = boost::dynamic_pointer_cast&lt;Derived&gt;(ptr);
</code></pre>
<p>wie gewohnt ist eben ptr.get()==NULL, wenne s nicht geklappt hat bzw !ptr==true</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1228867</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1228867</guid><dc:creator><![CDATA[muffmolch]]></dc:creator><pubDate>Wed, 14 Feb 2007 17:29:09 GMT</pubDate></item></channel></rss>