<?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[Frage zu shared_ptr aus der boost library und explicit cast]]></title><description><![CDATA[<p>Hallo zusammen!</p>
<p>Ich habe folgendes Stück Code</p>
<pre><code class="language-cpp">bool cBoard::addField(cFieldPtr field)
{
	// ....
	if (field-&gt;isTarget())
		mTargets.push_back( (cTargetFieldPtr) field ); // error C2440

	return true;	
}
</code></pre>
<p>Dabei ist deklariert</p>
<pre><code class="language-cpp">typedef boost::shared_ptr&lt; cField &gt; cFieldPtr;

      class cTargetField : public cField
      typedef boost::shared_ptr&lt; cTargetField &gt; cTargetFieldPtr;

      std::vector&lt; cTargetFieldPtr &gt; mTargets;
</code></pre>
<p>Offenbar funktioniert ein explizites Casting von shared_ptr nicht, wie ich es eigentlich gedacht habe. Der Microsoft Compiler gibt mir Fehler C2440 beim Kompilieren:</p>
<pre><code>error C2440: 'Initialisierung': 'nclib::adt::cField *const ' kann nicht in 'nclib::adt::cTargetField *' konvertiert werden
Umwandlung von der Basis in die Ableitung erfordert 'dynamic_cast' oder 'static_cast'
</code></pre>
<p>Stelle ich cTargetField* in den Vektor und benutze field.get(), so funktioniert es natürlich.</p>
<p>Ist es per se nicht möglich, explizit zu casten bei shared_ptr oder mache ich hier etwas falsch?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/184567/frage-zu-shared_ptr-aus-der-boost-library-und-explicit-cast</link><generator>RSS for Node</generator><lastBuildDate>Wed, 23 Sep 2026 18:04:05 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/184567.rss" rel="self" type="application/rss+xml"/><pubDate>Sat, 16 Jun 2007 16:34:26 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Frage zu shared_ptr aus der boost library und explicit cast on Sat, 16 Jun 2007 16:34:26 GMT]]></title><description><![CDATA[<p>Hallo zusammen!</p>
<p>Ich habe folgendes Stück Code</p>
<pre><code class="language-cpp">bool cBoard::addField(cFieldPtr field)
{
	// ....
	if (field-&gt;isTarget())
		mTargets.push_back( (cTargetFieldPtr) field ); // error C2440

	return true;	
}
</code></pre>
<p>Dabei ist deklariert</p>
<pre><code class="language-cpp">typedef boost::shared_ptr&lt; cField &gt; cFieldPtr;

      class cTargetField : public cField
      typedef boost::shared_ptr&lt; cTargetField &gt; cTargetFieldPtr;

      std::vector&lt; cTargetFieldPtr &gt; mTargets;
</code></pre>
<p>Offenbar funktioniert ein explizites Casting von shared_ptr nicht, wie ich es eigentlich gedacht habe. Der Microsoft Compiler gibt mir Fehler C2440 beim Kompilieren:</p>
<pre><code>error C2440: 'Initialisierung': 'nclib::adt::cField *const ' kann nicht in 'nclib::adt::cTargetField *' konvertiert werden
Umwandlung von der Basis in die Ableitung erfordert 'dynamic_cast' oder 'static_cast'
</code></pre>
<p>Stelle ich cTargetField* in den Vektor und benutze field.get(), so funktioniert es natürlich.</p>
<p>Ist es per se nicht möglich, explizit zu casten bei shared_ptr oder mache ich hier etwas falsch?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1307349</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1307349</guid><dc:creator><![CDATA[jamanikun]]></dc:creator><pubDate>Sat, 16 Jun 2007 16:34:26 GMT</pubDate></item><item><title><![CDATA[Reply to Frage zu shared_ptr aus der boost library und explicit cast on Sat, 16 Jun 2007 17:32:30 GMT]]></title><description><![CDATA[<p>Die Boost Smart-Ptr bieten eigene Methoden zum Casten:</p>
<p><a href="http://boost.org/libs/smart_ptr/shared_ptr.htm#static_pointer_cast" rel="nofollow">http://boost.org/libs/smart_ptr/shared_ptr.htm#static_pointer_cast</a></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1307367</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1307367</guid><dc:creator><![CDATA[The-Kenny]]></dc:creator><pubDate>Sat, 16 Jun 2007 17:32:30 GMT</pubDate></item><item><title><![CDATA[Reply to Frage zu shared_ptr aus der boost library und explicit cast on Sat, 16 Jun 2007 18:51:03 GMT]]></title><description><![CDATA[<p>Funktioniert, besten Dank!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1307406</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1307406</guid><dc:creator><![CDATA[jamanikun]]></dc:creator><pubDate>Sat, 16 Jun 2007 18:51:03 GMT</pubDate></item></channel></rss>