<?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 downcasten]]></title><description><![CDATA[<p>Hi ihrs!</p>
<p>Ich habe folgende Situation:<br />
Bei mir werden die Klassen &quot;texture&quot;, &quot;surface&quot; und &quot;sound&quot; von der Klasse &quot;resource&quot; abgeleitet und ich möchte mir nun einen resourcen-Manager schreiben um diese Klassen zu verwalten und nicht für jede Resource einen eigenen Manager zu schreiben.</p>
<p>Der kleinste gemeinsame Nenner aller Klassen ist &quot;resource&quot;, daher einen resourcen-Manager. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /> Ich habe mir also gedacht, dass es eine &quot;get&quot;-Methode in diesem Manager gibt und er mir einen resource-shared_ptr wiedergibt (oder ich es per Template bestimme). Da der Manager jedoch <strong>nur</strong> &quot;resource&quot; sichert (in einer Map) und nicht alle Abkömmlinge (sollen ja in Zukunft neue hinzu kommen) müsste ich also downcasten zu dem Typ der gefordert wird <strong>und</strong> bei der Erstellung mit new erstellt wurde.</p>
<p>Ein Up-Cast geht ohne Probleme, doch bei einem Downcast hagelt es fehler. Hier ein kleines Beispiel:</p>
<pre><code class="language-cpp">// textur erstellen
    boost::shared_ptr&lt;texture&gt; tex1 (new texture ());
    // upcast (funktioniert)
    boost::shared_ptr&lt;resource&gt; res1 (tex1);
    // downcast (geht nicht)
    boost::shared_ptr&lt;texture&gt; tex2 (res1);
</code></pre>
<p>Folgenden Fehler kommen</p>
<pre><code>1&gt;c:\programme\boost\boost_1_35_0\boost\shared_ptr.hpp(212) : error C2440: 'initializing' : cannot convert from 'resource *const ' to 'texture *'
1&gt;        Cast from base to derived requires dynamic_cast or static_cast
1&gt;        e:\test\main.cpp(63) : see reference to function template instantiation 'boost::shared_ptr&lt;T&gt;::shared_ptr&lt;resource&gt;(const boost::shared_ptr&lt;resource&gt; &amp;)' being compiled
1&gt;        with
1&gt;        [
1&gt;            T=texture
1&gt;        ]
1&gt;c:\programme\boost\boost_1_35_0\boost\shared_ptr.hpp(212) : error C2439: 'boost::shared_ptr&lt;T&gt;::px' : member could not be initialized
1&gt;        with
1&gt;        [
1&gt;            T=texture
1&gt;        ]
1&gt;        c:\programme\boost\boost_1_35_0\boost\shared_ptr.hpp(475) : see declaration of 'boost::shared_ptr&lt;T&gt;::px'
1&gt;        with
1&gt;        [
1&gt;            T=texture
1&gt;        ]
</code></pre>
<p>Also hab ich mal geguckt und folgendes probiert:</p>
<pre><code class="language-cpp">// textur erstellen
    boost::shared_ptr&lt;texture&gt; tex1 (new texture ());
    // upcast (funktioniert)
    boost::shared_ptr&lt;resource&gt; res1 (tex1);
    // downcast (&quot;funktioniert&quot;)
    boost::shared_ptr&lt;texture&gt; tex2 ((texture*)res1.get ());
</code></pre>
<p>Ja es ist ein very bad cast von res1 auf tex2, doch der Inhalt stimmt. Jedoch wird der Zähler von tex1 nicht inkrementiert und tex2 fängt wieder bei 1 an zu zählen, wodurch ich das shared_ptr ordentlich &quot;ausgehebelt&quot; habe, da get den &quot;echten Zeiger&quot; wiedergibt.</p>
<p>Also keine Hilfreiche Lösung.</p>
<p>Auch ein direkter Cast geht nicht:</p>
<pre><code class="language-cpp">// textur erstellen
    boost::shared_ptr&lt;texture&gt; tex1 (new texture ());
    // upcast (funktioniert)
    boost::shared_ptr&lt;resource&gt; res1 (tex1);
    // downcast (geht nicht)
    boost::shared_ptr&lt;texture&gt; tex2 ((boost::shared_ptr&lt;texture&gt;)res1);
</code></pre>
<p>und wird mir gedankt mit folgendem Fehler:</p>
<pre><code>1&gt;c:\programme\boost\boost_1_35_0\boost\shared_ptr.hpp(212) : error C2440: 'initializing' : cannot convert from 'resource *const ' to 'texture *'
1&gt;        Cast from base to derived requires dynamic_cast or static_cast
1&gt;        e:\test\main.cpp(66) : see reference to function template instantiation 'boost::shared_ptr&lt;T&gt;::shared_ptr&lt;resource&gt;(const boost::shared_ptr&lt;resource&gt; &amp;)' being compiled
1&gt;        with
1&gt;        [
1&gt;            T=texture
1&gt;        ]
1&gt;c:\programme\boost\boost_1_35_0\boost\shared_ptr.hpp(212) : error C2439: 'boost::shared_ptr&lt;T&gt;::px' : member could not be initialized
1&gt;        with
1&gt;        [
1&gt;            T=texture
1&gt;        ]
1&gt;        c:\programme\boost\boost_1_35_0\boost\shared_ptr.hpp(475) : see declaration of 'boost::shared_ptr&lt;T&gt;::px'
1&gt;        with
1&gt;        [
1&gt;            T=texture
1&gt;        ]
</code></pre>
<p>Hat jemand eine Idee wie ich das Problem lösen kann?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/223620/boost-shared_ptr-downcasten</link><generator>RSS for Node</generator><lastBuildDate>Sat, 03 Oct 2026 19:18:55 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/223620.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 28 Sep 2008 18:33:31 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to boost::shared_ptr downcasten on Sun, 28 Sep 2008 18:33:31 GMT]]></title><description><![CDATA[<p>Hi ihrs!</p>
<p>Ich habe folgende Situation:<br />
Bei mir werden die Klassen &quot;texture&quot;, &quot;surface&quot; und &quot;sound&quot; von der Klasse &quot;resource&quot; abgeleitet und ich möchte mir nun einen resourcen-Manager schreiben um diese Klassen zu verwalten und nicht für jede Resource einen eigenen Manager zu schreiben.</p>
<p>Der kleinste gemeinsame Nenner aller Klassen ist &quot;resource&quot;, daher einen resourcen-Manager. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /> Ich habe mir also gedacht, dass es eine &quot;get&quot;-Methode in diesem Manager gibt und er mir einen resource-shared_ptr wiedergibt (oder ich es per Template bestimme). Da der Manager jedoch <strong>nur</strong> &quot;resource&quot; sichert (in einer Map) und nicht alle Abkömmlinge (sollen ja in Zukunft neue hinzu kommen) müsste ich also downcasten zu dem Typ der gefordert wird <strong>und</strong> bei der Erstellung mit new erstellt wurde.</p>
<p>Ein Up-Cast geht ohne Probleme, doch bei einem Downcast hagelt es fehler. Hier ein kleines Beispiel:</p>
<pre><code class="language-cpp">// textur erstellen
    boost::shared_ptr&lt;texture&gt; tex1 (new texture ());
    // upcast (funktioniert)
    boost::shared_ptr&lt;resource&gt; res1 (tex1);
    // downcast (geht nicht)
    boost::shared_ptr&lt;texture&gt; tex2 (res1);
</code></pre>
<p>Folgenden Fehler kommen</p>
<pre><code>1&gt;c:\programme\boost\boost_1_35_0\boost\shared_ptr.hpp(212) : error C2440: 'initializing' : cannot convert from 'resource *const ' to 'texture *'
1&gt;        Cast from base to derived requires dynamic_cast or static_cast
1&gt;        e:\test\main.cpp(63) : see reference to function template instantiation 'boost::shared_ptr&lt;T&gt;::shared_ptr&lt;resource&gt;(const boost::shared_ptr&lt;resource&gt; &amp;)' being compiled
1&gt;        with
1&gt;        [
1&gt;            T=texture
1&gt;        ]
1&gt;c:\programme\boost\boost_1_35_0\boost\shared_ptr.hpp(212) : error C2439: 'boost::shared_ptr&lt;T&gt;::px' : member could not be initialized
1&gt;        with
1&gt;        [
1&gt;            T=texture
1&gt;        ]
1&gt;        c:\programme\boost\boost_1_35_0\boost\shared_ptr.hpp(475) : see declaration of 'boost::shared_ptr&lt;T&gt;::px'
1&gt;        with
1&gt;        [
1&gt;            T=texture
1&gt;        ]
</code></pre>
<p>Also hab ich mal geguckt und folgendes probiert:</p>
<pre><code class="language-cpp">// textur erstellen
    boost::shared_ptr&lt;texture&gt; tex1 (new texture ());
    // upcast (funktioniert)
    boost::shared_ptr&lt;resource&gt; res1 (tex1);
    // downcast (&quot;funktioniert&quot;)
    boost::shared_ptr&lt;texture&gt; tex2 ((texture*)res1.get ());
</code></pre>
<p>Ja es ist ein very bad cast von res1 auf tex2, doch der Inhalt stimmt. Jedoch wird der Zähler von tex1 nicht inkrementiert und tex2 fängt wieder bei 1 an zu zählen, wodurch ich das shared_ptr ordentlich &quot;ausgehebelt&quot; habe, da get den &quot;echten Zeiger&quot; wiedergibt.</p>
<p>Also keine Hilfreiche Lösung.</p>
<p>Auch ein direkter Cast geht nicht:</p>
<pre><code class="language-cpp">// textur erstellen
    boost::shared_ptr&lt;texture&gt; tex1 (new texture ());
    // upcast (funktioniert)
    boost::shared_ptr&lt;resource&gt; res1 (tex1);
    // downcast (geht nicht)
    boost::shared_ptr&lt;texture&gt; tex2 ((boost::shared_ptr&lt;texture&gt;)res1);
</code></pre>
<p>und wird mir gedankt mit folgendem Fehler:</p>
<pre><code>1&gt;c:\programme\boost\boost_1_35_0\boost\shared_ptr.hpp(212) : error C2440: 'initializing' : cannot convert from 'resource *const ' to 'texture *'
1&gt;        Cast from base to derived requires dynamic_cast or static_cast
1&gt;        e:\test\main.cpp(66) : see reference to function template instantiation 'boost::shared_ptr&lt;T&gt;::shared_ptr&lt;resource&gt;(const boost::shared_ptr&lt;resource&gt; &amp;)' being compiled
1&gt;        with
1&gt;        [
1&gt;            T=texture
1&gt;        ]
1&gt;c:\programme\boost\boost_1_35_0\boost\shared_ptr.hpp(212) : error C2439: 'boost::shared_ptr&lt;T&gt;::px' : member could not be initialized
1&gt;        with
1&gt;        [
1&gt;            T=texture
1&gt;        ]
1&gt;        c:\programme\boost\boost_1_35_0\boost\shared_ptr.hpp(475) : see declaration of 'boost::shared_ptr&lt;T&gt;::px'
1&gt;        with
1&gt;        [
1&gt;            T=texture
1&gt;        ]
</code></pre>
<p>Hat jemand eine Idee wie ich das Problem lösen kann?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1589823</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1589823</guid><dc:creator><![CDATA[C++&#x27;ler]]></dc:creator><pubDate>Sun, 28 Sep 2008 18:33:31 GMT</pubDate></item><item><title><![CDATA[Reply to boost::shared_ptr downcasten on Sun, 28 Sep 2008 18:40:32 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">boost::shared_ptr&lt;resource&gt; res1 (boost::dynamic_pointer_cast&lt;resource&gt;(tex1));
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1589827</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1589827</guid><dc:creator><![CDATA[camper]]></dc:creator><pubDate>Sun, 28 Sep 2008 18:40:32 GMT</pubDate></item><item><title><![CDATA[Reply to boost::shared_ptr downcasten on Sun, 28 Sep 2008 18:45:23 GMT]]></title><description><![CDATA[<p>oh mein gott ihr seid so gut! Danke es geht und der Referenzzähler ist auch richtig, ich liebe euch <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1589830</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1589830</guid><dc:creator><![CDATA[C++&#x27;ler]]></dc:creator><pubDate>Sun, 28 Sep 2008 18:45:23 GMT</pubDate></item></channel></rss>