<?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[Dynamischer Type-Cast]]></title><description><![CDATA[<p>Hi,</p>
<p>ich bekomme einen Zeiger auf einen Typen übergeben, von dem mehrere andere Typen abgeleitet werden. Jetzt möchte ich überprüfen, ob dieser Zeiger auf einen bestimmten abgeleiteten Typ und ihn dann Casten. Ungefähr so:</p>
<pre><code>try {
  MyPointer = dynamic_cast&lt;TMyPointer*&gt;(MyPara);
} catch (...) {
  throw &quot;Wrong type&quot;;
}
</code></pre>
<p>Jetzt kann MyPointer aber von irgendeinem der abgeleiteten Typen sein, sodass ich den Cast-Typ ermitten muss. Im Prinzip so:</p>
<pre><code>try {
  MyPointer = dynamic_cast&lt;typeof(MyPointer)&gt;(MyPara);
} catch (...) {
  throw &quot;Wrong type&quot;;
}
</code></pre>
<p>Geht das und wenn ja, wie?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/270133/dynamischer-type-cast</link><generator>RSS for Node</generator><lastBuildDate>Sun, 30 Aug 2026 13:46:31 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/270133.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 07 Jul 2010 07:29:41 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Dynamischer Type-Cast on Wed, 07 Jul 2010 07:29:41 GMT]]></title><description><![CDATA[<p>Hi,</p>
<p>ich bekomme einen Zeiger auf einen Typen übergeben, von dem mehrere andere Typen abgeleitet werden. Jetzt möchte ich überprüfen, ob dieser Zeiger auf einen bestimmten abgeleiteten Typ und ihn dann Casten. Ungefähr so:</p>
<pre><code>try {
  MyPointer = dynamic_cast&lt;TMyPointer*&gt;(MyPara);
} catch (...) {
  throw &quot;Wrong type&quot;;
}
</code></pre>
<p>Jetzt kann MyPointer aber von irgendeinem der abgeleiteten Typen sein, sodass ich den Cast-Typ ermitten muss. Im Prinzip so:</p>
<pre><code>try {
  MyPointer = dynamic_cast&lt;typeof(MyPointer)&gt;(MyPara);
} catch (...) {
  throw &quot;Wrong type&quot;;
}
</code></pre>
<p>Geht das und wenn ja, wie?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1922535</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1922535</guid><dc:creator><![CDATA[Heimelchen]]></dc:creator><pubDate>Wed, 07 Jul 2010 07:29:41 GMT</pubDate></item><item><title><![CDATA[Reply to Dynamischer Type-Cast on Wed, 07 Jul 2010 07:39:31 GMT]]></title><description><![CDATA[<p>Geht nicht. Was genau willst du erreichen?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1922537</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1922537</guid><dc:creator><![CDATA[Nukularfüsiker]]></dc:creator><pubDate>Wed, 07 Jul 2010 07:39:31 GMT</pubDate></item><item><title><![CDATA[Reply to Dynamischer Type-Cast on Wed, 07 Jul 2010 07:40:05 GMT]]></title><description><![CDATA[<p>Kleine Templatezauberei:</p>
<pre><code class="language-cpp">template&lt; typename To, typename From &gt;
void inplace_dynamic_cast( To*&amp; to, From* from )
{
    to = dynamic_cast&lt; To* &gt;( from );
}

inplace_dynamic_cast( MyPointer, MyPara );
</code></pre>
<p>BTW: Wenn Du beim Cast eine Exception fangen möchtest, musst Du Referenzen statt Pointer casten.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1922538</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1922538</guid><dc:creator><![CDATA[LordJaxom]]></dc:creator><pubDate>Wed, 07 Jul 2010 07:40:05 GMT</pubDate></item><item><title><![CDATA[Reply to Dynamischer Type-Cast on Wed, 07 Jul 2010 07:41:08 GMT]]></title><description><![CDATA[<p>Hallo,</p>
<p>Erstmal: Ein fehlgeschlagener dynamic_cast auf einen Pointer wirft keine Exception sondern gibt 0 zurück. Du musst also darauf testen.<br />
typeof gibt es im aktuell gültigen Standard nicht. Was willst du damit eigentlich erreichen?<br />
Evtl. gibt es einen anderen Weg.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1922539</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1922539</guid><dc:creator><![CDATA[Braunstein]]></dc:creator><pubDate>Wed, 07 Jul 2010 07:41:08 GMT</pubDate></item><item><title><![CDATA[Reply to Dynamischer Type-Cast on Wed, 07 Jul 2010 07:52:07 GMT]]></title><description><![CDATA[<p>Suchst du hier eventuell die virtuellen Funktionen?</p>
<pre><code class="language-cpp">class foo
{
public:
   virtual void func() { cout &lt;&lt; &quot;foo&quot; &lt;&lt; endl; }
};

class bar : public foo
{
public:
   virtual void func() { cout &lt;&lt; &quot;bar&quot; &lt;&lt; endl; }
};

// ...
foo *ptr = new bar;
ptr-&gt;func(); // Ausgabe: bar
</code></pre>
<p>Sonst ergibt das nämlich nicht viel Sinn. Was hast du davon, wenn du den Pointer in einen Typen umwandeln kannst, den du gar nicht kennst? Entweder du willst auf Member zugreifen, die im Basisklaseninterface definiert sind (-&gt; virtuelle Funktionen) oder auf typspezifische Member, die du aber gar nicht kennen kannst.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1922543</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1922543</guid><dc:creator><![CDATA[ipsec]]></dc:creator><pubDate>Wed, 07 Jul 2010 07:52:07 GMT</pubDate></item><item><title><![CDATA[Reply to Dynamischer Type-Cast on Wed, 07 Jul 2010 08:07:59 GMT]]></title><description><![CDATA[<p>Braunstein schrieb:</p>
<blockquote>
<p>Erstmal: Ein fehlgeschlagener dynamic_cast auf einen Pointer wirft keine Exception sondern gibt 0 zurück.</p>
</blockquote>
<p>Zitat aus &quot;C++ Primer&quot;:</p>
<blockquote>
<p>(...), when a cast fails, it throws a std::bad_cast exception.</p>
</blockquote>
<p>Hört sich für mich schon so an...</p>
<p>Der Hintergrund ist: Ich habe ein Frame, dessen Konstruktor einen Owner (TComponent) übergeben bekommt. Diesen Owner speichere ich entsprechend gecastet in einer privaten Eigenschaft, da ich bestimmte Komponenten von ihm verwende (und bei ihm voraussetze). Wird das Formular jetzt woanders verwendet, möchte ich nur die Definition der privaten Eigenschaft ändern, sonst nix. In dem Konstruktor möchte ich also den Owner-Parameter auf den Typ von PrivateOwner casten um ihn dann in selbigen zu speichern.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1922547</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1922547</guid><dc:creator><![CDATA[Heimelchen]]></dc:creator><pubDate>Wed, 07 Jul 2010 08:07:59 GMT</pubDate></item><item><title><![CDATA[Reply to Dynamischer Type-Cast on Wed, 07 Jul 2010 08:13:49 GMT]]></title><description><![CDATA[<p>Wenn das schon in deinem Buch stand, hattest du doch bereits eine Antwort auf deine Frage.<br />
Die Exception wird jedoch nur bei Referenzen geworfen, bei Zeigern liefert dynamic_cast eben einfach 0.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1922549</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1922549</guid><dc:creator><![CDATA[Athar]]></dc:creator><pubDate>Wed, 07 Jul 2010 08:13:49 GMT</pubDate></item><item><title><![CDATA[Reply to Dynamischer Type-Cast on Wed, 07 Jul 2010 08:16:45 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">class virtUal 
{
    public:

        virtUal(int a) : mi(a) {}
        virtUal ~virtUal();

        virtual void virt() = 0;
        int i() {return mi;}

    protected:
        int mi;
};

class ab1 : public virtUal 
{
    public:
        ab1(int i) : virtUal(i) {};
        void virt() { mi = 1000; }
};

class ab2 : public virtUal 
{
    public:
        ab2(int i) : virtUal(i) {};
        void virt() { mi = 0; }
};

int main()
{
    virtUal * v1 =  new ab1(5);

    ab1 * a = dynamic_cast&lt;ab1*&gt;(v1);   // &lt;&lt;-- kein Nulpointer
    ab2 * a2 = dynamic_cast&lt;ab2*&gt;(v1); //  &lt;&lt;-- Nulpointer

    ab1 a3 = dynamic_cast&lt;ab1&gt;(*v1);   // &lt;&lt;-- gültiges objekt
    ab2 a4 = dynamic_cast&lt;ab2&gt;(*v1);   // &lt;&lt;-- crash .. std::bad_cast exception
}
</code></pre>
<p>Du könntest nach allen möglichen abgeleiteten Klassen Probe-casten und das nehmen das keinen Nullpointer zurückgibt...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1922551</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1922551</guid><dc:creator><![CDATA[padreigh]]></dc:creator><pubDate>Wed, 07 Jul 2010 08:16:45 GMT</pubDate></item><item><title><![CDATA[Reply to Dynamischer Type-Cast on Wed, 07 Jul 2010 08:34:30 GMT]]></title><description><![CDATA[<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/24752">@Heimelchen</a><br />
bezüglich der Exception siehe Standard 5.2.7 9</p>
<blockquote>
<p>The value of a failed cast to pointer type is the null pointer value of the required result type. A failed cast to<br />
reference type throws bad_cast (18.5.2).</p>
</blockquote>
<p>Ich würde dir vorschlagen aus deinem Frame eine Template Klasse zu machen wenn es denn wirklich nötig ist den genauen Typ zu können.<br />
Ich frage mich allerdings warum das nötig ist. Wenn du den genauen Typ des Owners wisse willst, dann willst du vielleicht auf Membervaraiblen oder -funktionen zugreifen die nur da existieren.<br />
Dann hättest du aber auch unterschiedlichen Code je nach Owner-Typ. Das sieht mir dann eher weider danach aus einfach speziell dafür angepasste Frames zu nehmen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1922560</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1922560</guid><dc:creator><![CDATA[Braunstein]]></dc:creator><pubDate>Wed, 07 Jul 2010 08:34:30 GMT</pubDate></item><item><title><![CDATA[Reply to Dynamischer Type-Cast on Wed, 07 Jul 2010 08:49:33 GMT]]></title><description><![CDATA[<p>Ich glaube, bevor ich da einen riesen Aufwand betreibe, finde ich mich lieber damit ab, im Änderungsfall an zwei Stellen einen neuen Typ eingeben zu müssen... :-7</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1922571</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1922571</guid><dc:creator><![CDATA[Heimelchen]]></dc:creator><pubDate>Wed, 07 Jul 2010 08:49:33 GMT</pubDate></item><item><title><![CDATA[Reply to Dynamischer Type-Cast on Wed, 07 Jul 2010 08:55:17 GMT]]></title><description><![CDATA[<p>Sofern der Typ von MyPointer wirklich ein Zeiger auf den konkreten Typ ist, was spricht gegen die inplace Lösung?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1922575</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1922575</guid><dc:creator><![CDATA[LordJaxom]]></dc:creator><pubDate>Wed, 07 Jul 2010 08:55:17 GMT</pubDate></item><item><title><![CDATA[Reply to Dynamischer Type-Cast on Wed, 07 Jul 2010 09:12:56 GMT]]></title><description><![CDATA[<p>Hachja, es lebe die Frickelei <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f603.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--grinning_face_with_big_eyes"
      title=":D"
      alt="😃"
    /></p>
<pre><code class="language-cpp">#include &lt;typeinfo&gt;
#include &lt;iostream&gt;
#include &lt;string&gt;

class X
{
public:
	virtual std::string GetType()
	{
		return typeid(*this).name();
	}
};

class A : public X
{
};

class B : public X
{
};

int main()
{
	X *a = new A;

	std::cout &lt;&lt; a-&gt;GetType() &lt;&lt; std::endl; // vom Typ &quot;class A&quot;

	delete a;

	a = new B;

	std::cout &lt;&lt; a-&gt;GetType() &lt;&lt; std::endl; // vom Typ &quot;class B&quot;

	delete a;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1922581</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1922581</guid><dc:creator><![CDATA[..........]]></dc:creator><pubDate>Wed, 07 Jul 2010 09:12:56 GMT</pubDate></item><item><title><![CDATA[Reply to Dynamischer Type-Cast on Wed, 07 Jul 2010 11:15:00 GMT]]></title><description><![CDATA[<p>Du erzeugst wegen des fehlenden virtuellen Destruktors in deinem Beispiel undefiniertes Verhalten.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1922653</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1922653</guid><dc:creator><![CDATA[Nexus]]></dc:creator><pubDate>Wed, 07 Jul 2010 11:15:00 GMT</pubDate></item><item><title><![CDATA[Reply to Dynamischer Type-Cast on Wed, 07 Jul 2010 11:19:02 GMT]]></title><description><![CDATA[<p>Ist das hier wirklich der Fall?<br />
Keine dieser Klassen hat irgendwelche Membervariablen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1922657</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1922657</guid><dc:creator><![CDATA[Braunstein]]></dc:creator><pubDate>Wed, 07 Jul 2010 11:19:02 GMT</pubDate></item><item><title><![CDATA[Reply to Dynamischer Type-Cast on Wed, 07 Jul 2010 11:31:17 GMT]]></title><description><![CDATA[<p>Braunstein schrieb:</p>
<blockquote>
<p>Ist das hier wirklich der Fall?<br />
Keine dieser Klassen hat irgendwelche Membervariablen.</p>
</blockquote>
<p>Aus dem C++-Standard:</p>
<p>5.3.4/3 schrieb:</p>
<blockquote>
<p>In the first alternative (delete object), if the static type of the operand is different from its dynamic type, the static type shall be a base class of the operand’s dynamic type <strong>and the static type shall have a virtual destructor or the behavior is undefined</strong>. In the second alternative (delete array) if the dynamic type of the object to be deleted differs from its static type, the behavior is undefined.73)</p>
</blockquote>
<p>In der Praxis wird es vielleicht wegen der fehlenden Member funktionieren, aber es besteht keine Garantie. Ausserdem ist es sehr schlechter Stil, polymorph genutze Klassen ohne virtuellen Destruktor auszustatten. Nicht zuletzt habe ich das angemerkt, weil ich kürzlich einige Stunden wegen genau diesem idiotischen Fehler debuggt habe. <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/1922663</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1922663</guid><dc:creator><![CDATA[Nexus]]></dc:creator><pubDate>Wed, 07 Jul 2010 11:31:17 GMT</pubDate></item><item><title><![CDATA[Reply to Dynamischer Type-Cast on Wed, 07 Jul 2010 11:52:03 GMT]]></title><description><![CDATA[<p>Vielen Dank für die Information.</p>
<p>Ich habe sowas auch noch nie gemacht. Es gibt aber bei uns Kollegen die meinen das sei kein Problem. Deswegen die Frage.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1922670</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1922670</guid><dc:creator><![CDATA[Braunstein]]></dc:creator><pubDate>Wed, 07 Jul 2010 11:52:03 GMT</pubDate></item></channel></rss>