<?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[Welcher Cast von void* auf T*]]></title><description><![CDATA[<p>Abend,</p>
<p>wenn ich von void* auf T* casten muss, welchen cast benutz ich dann? static_cast oder dynamic_cast?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/287859/welcher-cast-von-void-auf-t</link><generator>RSS for Node</generator><lastBuildDate>Thu, 20 Aug 2026 06:02:15 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/287859.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 05 Jun 2011 21:29:19 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Welcher Cast von void* auf T* on Sun, 05 Jun 2011 21:29:19 GMT]]></title><description><![CDATA[<p>Abend,</p>
<p>wenn ich von void* auf T* casten muss, welchen cast benutz ich dann? static_cast oder dynamic_cast?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2073822</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2073822</guid><dc:creator><![CDATA[Casterr]]></dc:creator><pubDate>Sun, 05 Jun 2011 21:29:19 GMT</pubDate></item><item><title><![CDATA[Reply to Welcher Cast von void* auf T* on Sun, 05 Jun 2011 21:30:50 GMT]]></title><description><![CDATA[<p><code>static_cast</code> .</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2073824</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2073824</guid><dc:creator><![CDATA[Nexus]]></dc:creator><pubDate>Sun, 05 Jun 2011 21:30:50 GMT</pubDate></item><item><title><![CDATA[Reply to Welcher Cast von void* auf T* on Mon, 06 Jun 2011 06:55:00 GMT]]></title><description><![CDATA[<p>dynamic_cast ist um einen zeiger auf eine basisklasse auf einen zeiger auf ein derivat zu casten.</p>
<p>static_cast konvertiert zwischen den datentypen hin und her</p>
<p>und reinterpret_cast interpretiert das an der übergebenen speicheraddresse vorhandene bitmuster als anderen datentypen</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2073888</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2073888</guid><dc:creator><![CDATA[Skym0sh0]]></dc:creator><pubDate>Mon, 06 Jun 2011 06:55:00 GMT</pubDate></item><item><title><![CDATA[Reply to Welcher Cast von void* auf T* on Mon, 06 Jun 2011 07:35:50 GMT]]></title><description><![CDATA[<p>Bedenke, dass</p>
<pre><code>T* ----------&gt; void* ----------&gt; T*
        |                 |
   static_cast       static_cast
  oder implizit
</code></pre>
<p>problemlos so funktioniert, wie man es gerne hätte (verlustfreie Rundreise) wohin gegen so etwas wie</p>
<pre><code>Derived* ----------&gt; void* ----------&gt; Base*               (Falsch!)
              |                 |
         static_cast       static_cast
        oder implizit
</code></pre>
<p><em>nicht</em> dasselbe ist wie</p>
<pre><code>Derived* --&gt; Base* ----------&gt; void* ----------&gt; Base*     (Richtig!)
          \______       |                 |
                 \ static_cast       static_cast
                  oder implizit
</code></pre>
<p>Mit anderen Worten: Du darfst einen void* nur dorthin zurückkonvertieren, woher er genommen ist (wenn man const und volatile mal ignoriert).</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2073892</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2073892</guid><dc:creator><![CDATA[ptr_conversion]]></dc:creator><pubDate>Mon, 06 Jun 2011 07:35:50 GMT</pubDate></item><item><title><![CDATA[Reply to Welcher Cast von void* auf T* on Mon, 06 Jun 2011 09:52:58 GMT]]></title><description><![CDATA[<p>Skym0sh0 schrieb:</p>
<blockquote>
<p>dynamic_cast ist um einen zeiger auf eine basisklasse auf einen zeiger auf ein derivat zu casten.</p>
</blockquote>
<p>Wie <code>static_cast</code></p>
<p>Skym0sh0 schrieb:</p>
<blockquote>
<p>static_cast konvertiert zwischen den datentypen hin und her</p>
</blockquote>
<p>Wie <code>const_cast</code> , <code>dynamic_cast</code> , <code>reinterpret_cast</code></p>
<p>ptr_conversion schrieb:</p>
<blockquote>
<p>[...]</p>
</blockquote>
<p>Gut erklärt! Als Ergänzung sei vielleicht noch gesagt, dass man <code>void*</code> relativ selten benötigt. Z.B. für Low-Level-Speicherverwaltung oder als eine Art Type Erasure, die zu weniger Template-Bloat führt (wie z.B. in Boost.PtrContainer).</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2073935</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2073935</guid><dc:creator><![CDATA[Nexus]]></dc:creator><pubDate>Mon, 06 Jun 2011 09:52:58 GMT</pubDate></item><item><title><![CDATA[Reply to Welcher Cast von void* auf T* on Mon, 06 Jun 2011 13:56:53 GMT]]></title><description><![CDATA[<p>Hallo,<br />
ich habe im Moment auch ein Cast-Problem. Ich schreib es mal hier rein, da es hier eh schon ums Casten geht:</p>
<p>Folgendes habe ich (nur beispielhaft):</p>
<pre><code class="language-cpp">struct base
{
	int a;
}

struct derived1 : public base
{
	std::vector&lt;int&gt; b;
}

struct derived2 : public base
{
	int c;
}
</code></pre>
<p>(in echt sind die Structs natürlich um einiges komplexer und es gibt mehr abgeleitete).</p>
<p>Nun habe ich ein Methode die viele Dateien läd und je nach Informationen in die verschiedenen derived Structs schreibt. Folgendes kommt dann zurück:</p>
<pre><code class="language-cpp">std::vector&lt;std::pair&lt;std::string, std::vector&lt;base&gt;&gt;&gt;
//std::string ist der Typname, und im Vektor werden alle Struktur-Objekte des Typs gespeichert
</code></pre>
<p>Nach dem mein Hauptprogramm nun die Daten geladen hat und in diesem Konstrukt hat, will es durch diese iterieren und nutzen.</p>
<pre><code class="language-cpp">std::vector&lt;std::pair&lt;std::string, std::vector&lt;base&gt;&gt;&gt; resultVector = loadAll();
for(std::vector&lt;std::pair&lt;std::string, std::vector&lt;base&gt;&gt;&gt;::iterator iter = resultVector.start; ...)
{
	for(std::vector&lt;base&gt; iter2 = iter-&gt;second.begin(); ...)
	{
		if(iter-&gt;first == &quot;1&quot;)
			//hier weiß ich, dass es derived1 sein muss
		else if(iter-&gt;first == &quot;2&quot;)
			//hier weiß ich, dass es derived2 sein muss
	}
}
</code></pre>
<p>Das ist wieder so weit runter gebrochen, dass das wichtigste da ist. z.b. wird normalerweise nicht direkt vorm durch iterieren erst geladen.</p>
<p>So aber jetzt muss ich base nach derived1 casten können und genau das schaffe ich nicht. hab es schon mit dynamic_cast, static_cast und explizite Conversion versucht aber alles klappt nicht.<br />
dynamic_cast will nur derived zu base machen aber nicht umgekehrt.</p>
<p>z.B. der static_cast:</p>
<pre><code class="language-cpp">derived1* strategy = static_cast&lt;derived1*&gt;(&amp;(*iter2));
</code></pre>
<p>Kompiliert aber nach dem Casten steht Müll drin.</p>
<p>Wie mache ich es richtig?</p>
<p>Danke für eure Hilfe</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2074015</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2074015</guid><dc:creator><![CDATA[Therese]]></dc:creator><pubDate>Mon, 06 Jun 2011 13:56:53 GMT</pubDate></item><item><title><![CDATA[Reply to Welcher Cast von void* auf T* on Mon, 06 Jun 2011 13:58:35 GMT]]></title><description><![CDATA[<p>Hier findet Slicing statt, da du die Objekte per Value in den vector speicherst. Du musst Zeiger verwenden, dafür eignet sich z.B. boost::ptr_vector oder ein std::vector&lt;unique_ptr&lt;T&gt;&gt;.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2074017</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2074017</guid><dc:creator><![CDATA[314159265358979]]></dc:creator><pubDate>Mon, 06 Jun 2011 13:58:35 GMT</pubDate></item><item><title><![CDATA[Reply to Welcher Cast von void* auf T* on Mon, 06 Jun 2011 14:17:08 GMT]]></title><description><![CDATA[<p>danke<br />
so hats funktioniert. wollte um das &quot;new&quot; rum kommen.</p>
<p>Habe nun einen</p>
<pre><code class="language-cpp">std::vector&lt;std::pair&lt;std::string, std::vector&lt;base*&gt;&gt;&gt;
</code></pre>
<p>was haben boost::ptr_vector und std::vector&lt;unique_ptr&lt;T&gt;&gt; für Vorteile?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2074020</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2074020</guid><dc:creator><![CDATA[Therese]]></dc:creator><pubDate>Mon, 06 Jun 2011 14:17:08 GMT</pubDate></item><item><title><![CDATA[Reply to Welcher Cast von void* auf T* on Mon, 06 Jun 2011 14:29:35 GMT]]></title><description><![CDATA[<p>Auch wenn ich etwas zu langsam war:<br />
Kuck dir mal folgendes an:</p>
<pre><code class="language-cpp">#include &lt;vector&gt;

class FooBar{
public:
        virtual ~FooBar(){} // Deine Basisklasse braucht eine virtuelle Funktion oder Destruktor für einen dynamic_cast
};
class Foo : public FooBar{
        void foo(){}
}
class Bar : public FooBar{
        void bar(){}
};

int main(){
        std::vector&lt;FooBar*&gt; vec; // Du brauchst einen Vektor von Pointern, da sonst der Copy-Konstruktor der Basis-Klasse aufgerufen wird - Alle Informationen der abgeleiteten Klassen incl. v-table gehen verloren
        vec.push_back(new Foo());
        vec.push_back(new Bar());
        for(std::vector&lt;FooBar*&gt;::const_iterator it = vec.begin(); it!=vec.end(); ++it){
                if(Foo* foo = dynamic_cast&lt;Foo*&gt;(*it)){ // so kannst du testen, ob ein Zeiger auf ein Objekt von einem bestimmten Typ ist - dynamic_cast liefert 0, wenn es nicht der Fall ist
                        foo-&gt;foo();
                }
                if(Bar* bar = dynamic_cast&lt;Bar*&gt;(*it)){
                        bar-&gt;bar();
                }
        }
        // Löschen der Objekte nicht vergessen, weswegen sich evtl. ein boost::ptr_vector anbietet, der alle Objekte darin löscht, wenn er zerstört wird (siehe PI)
}
</code></pre>
<p>EDIT: Wenn dynamic_cast ohne Zeiger verwendet wird, fliegt, falls es nicht möglich ist, eine bad_cast Exception.</p>
<p>Gruß,<br />
XSpille</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2074022</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2074022</guid><dc:creator><![CDATA[XSpille]]></dc:creator><pubDate>Mon, 06 Jun 2011 14:29:35 GMT</pubDate></item><item><title><![CDATA[Reply to Welcher Cast von void* auf T* on Mon, 06 Jun 2011 14:22:18 GMT]]></title><description><![CDATA[<p>Therese schrieb:</p>
<blockquote>
<p>was haben boost::ptr_vector und std::vector&lt;unique_ptr&lt;T&gt;&gt; für Vorteile?</p>
</blockquote>
<p>Du brauchst dich nicht um das delete kümmern, das wird dann für dich automatisch erledigt.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2074024</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2074024</guid><dc:creator><![CDATA[314159265358979]]></dc:creator><pubDate>Mon, 06 Jun 2011 14:22:18 GMT</pubDate></item><item><title><![CDATA[Reply to Welcher Cast von void* auf T* on Mon, 06 Jun 2011 14:36:27 GMT]]></title><description><![CDATA[<p>314159265358979 schrieb:</p>
<blockquote>
<p>Therese schrieb:</p>
<blockquote>
<p>was haben boost::ptr_vector und std::vector&lt;unique_ptr&lt;T&gt;&gt; für Vorteile?</p>
</blockquote>
<p>Du brauchst dich nicht um das delete kümmern, das wird dann für dich automatisch erledigt.</p>
</blockquote>
<p>Falls du boost verwenden darfst, würde ich boost::ptr_vector verwenden, da std::vector&lt;unique_ptr&lt;T&gt;&gt; afaik keine Vorteile hat (außer dass man 0 drin speichern kann).</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2074034</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2074034</guid><dc:creator><![CDATA[XSpille]]></dc:creator><pubDate>Mon, 06 Jun 2011 14:36:27 GMT</pubDate></item><item><title><![CDATA[Reply to Welcher Cast von void* auf T* on Mon, 06 Jun 2011 14:38:09 GMT]]></title><description><![CDATA[<p>Im Normalfall besser als std::vector&lt;std::shared_ptr&lt;T&gt;&gt; <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";)"
      alt="😉"
    /><br />
Aber ja, boost::ptr_vector ist zu bevorzugen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2074036</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2074036</guid><dc:creator><![CDATA[314159265358979]]></dc:creator><pubDate>Mon, 06 Jun 2011 14:38:09 GMT</pubDate></item><item><title><![CDATA[Reply to Welcher Cast von void* auf T* on Mon, 06 Jun 2011 14:43:56 GMT]]></title><description><![CDATA[<p>314159265358979 schrieb:</p>
<blockquote>
<p>Im Normalfall besser als std::vector&lt;std::shared_ptr&lt;T&gt;&gt; <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";)"
      alt="😉"
    /><br />
Aber ja, boost::ptr_vector ist zu bevorzugen.</p>
</blockquote>
<p>Bring ihn nicht auf blöde Gedanken mit std::vector&lt;std::shared_ptr&lt;T&gt;&gt;, <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";)"
      alt="😉"
    /><br />
sonst ist hier bald wieder jemand neues im Forum, der boost::shared_ptr&lt;T&gt; bzw. std::shared_ptr&lt;T&gt; als universellen 'Problemlöser' nutzt und Overhead in Kauf nimmt, den er in den seltensten Fällen nicht benötigt <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f44e.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--thumbs_down"
      title=":-1:"
      alt="👎"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2074041</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2074041</guid><dc:creator><![CDATA[XSpille]]></dc:creator><pubDate>Mon, 06 Jun 2011 14:43:56 GMT</pubDate></item></channel></rss>