<?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[C-Cast vs. C++ Cast]]></title><description><![CDATA[<p>Hi,</p>
<p>wenn ich den 4. Parameter auf (void**) caste, geht es einwandfrei:</p>
<pre><code class="language-cpp">DirectInput8Create(GetModuleHandle(NULL),
                        DIRECTINPUT_VERSION,
                        IID_IDirectInput8,
                        (void**)&amp;inputDevice,
                         NULL)
</code></pre>
<p>Der formale 4. Parameter ist vom Typ LPVOID*.<br />
Wenn ich die selbe Zeile als C++ cast schreibe, gibts nen Fehler</p>
<pre><code class="language-cpp">FAILED(DirectInput8Create(GetModuleHandle(NULL),
                             DIRECTINPUT_VERSION,
                             IID_IDirectInput8,
                             static_cast&lt;void**&gt;(&amp;inputDevice),
                             NULL)
</code></pre>
<blockquote>
<p>Fehler 1 error C2440: 'static_cast': 'LPDIRECTINPUT8 *__w64 ' kann nicht in 'void **' konvertiert werden e:\programmieren\c++\directx 9\environment rendering\oneil scattering\v3\dxapplication.cpp 58</p>
</blockquote>
<p>WTF!? Wieso ist das so?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/192906/c-cast-vs-c-cast</link><generator>RSS for Node</generator><lastBuildDate>Sun, 27 Sep 2026 17:26:54 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/192906.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 19 Sep 2007 20:48:02 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to C-Cast vs. C++ Cast on Wed, 19 Sep 2007 20:48:02 GMT]]></title><description><![CDATA[<p>Hi,</p>
<p>wenn ich den 4. Parameter auf (void**) caste, geht es einwandfrei:</p>
<pre><code class="language-cpp">DirectInput8Create(GetModuleHandle(NULL),
                        DIRECTINPUT_VERSION,
                        IID_IDirectInput8,
                        (void**)&amp;inputDevice,
                         NULL)
</code></pre>
<p>Der formale 4. Parameter ist vom Typ LPVOID*.<br />
Wenn ich die selbe Zeile als C++ cast schreibe, gibts nen Fehler</p>
<pre><code class="language-cpp">FAILED(DirectInput8Create(GetModuleHandle(NULL),
                             DIRECTINPUT_VERSION,
                             IID_IDirectInput8,
                             static_cast&lt;void**&gt;(&amp;inputDevice),
                             NULL)
</code></pre>
<blockquote>
<p>Fehler 1 error C2440: 'static_cast': 'LPDIRECTINPUT8 *__w64 ' kann nicht in 'void **' konvertiert werden e:\programmieren\c++\directx 9\environment rendering\oneil scattering\v3\dxapplication.cpp 58</p>
</blockquote>
<p>WTF!? Wieso ist das so?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1368859</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1368859</guid><dc:creator><![CDATA[BuffCaster]]></dc:creator><pubDate>Wed, 19 Sep 2007 20:48:02 GMT</pubDate></item><item><title><![CDATA[Reply to C-Cast vs. C++ Cast on Wed, 19 Sep 2007 21:20:18 GMT]]></title><description><![CDATA[<p>Wie wäre es mit der Holzhammer-Methode (reinterpret_cast&lt;&gt;())</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1368882</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1368882</guid><dc:creator><![CDATA[The-Kenny]]></dc:creator><pubDate>Wed, 19 Sep 2007 21:20:18 GMT</pubDate></item><item><title><![CDATA[Reply to C-Cast vs. C++ Cast on Wed, 19 Sep 2007 21:23:03 GMT]]></title><description><![CDATA[<p>der c-cast interessiert sich nur mäßig genau für das, was du tust. static_cast hingegen achtet darauf, ob es überhaupt möglich ist und daran scheitert es hier. eine referenz kann man als zeiger interpretieren aber nicht als einen zeiger auf einen zeiger, wie du es getan hast.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1368885</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1368885</guid><dc:creator><![CDATA[ghorst]]></dc:creator><pubDate>Wed, 19 Sep 2007 21:23:03 GMT</pubDate></item><item><title><![CDATA[Reply to C-Cast vs. C++ Cast on Wed, 19 Sep 2007 21:24:35 GMT]]></title><description><![CDATA[<p>reinterpret_cast ist nicht die holzhammer methode, sondern die &quot;korrekte&quot;. der cast ist nämlich von vornherein nicht besonders toll gewesen <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="😉"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1368887</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1368887</guid><dc:creator><![CDATA[thordk]]></dc:creator><pubDate>Wed, 19 Sep 2007 21:24:35 GMT</pubDate></item><item><title><![CDATA[Reply to C-Cast vs. C++ Cast on Wed, 19 Sep 2007 21:45:45 GMT]]></title><description><![CDATA[<p>portabel wäre:</p>
<pre><code class="language-cpp">void* inputDevice_tmp = 0; // oder inputDevice
FAILED(DirectInput8Create(GetModuleHandle(NULL),
                             DIRECTINPUT_VERSION,
                             IID_IDirectInput8,
                             &amp;inputDevice,
                             NULL)
inputDevice = static_cast&lt;LPDIRECTINPUT8&gt;(inputDevice_tmp);
</code></pre>
<p>was passiert mit reinterpret_cast bzw. C-Cast? Das ist äquivalent zu:</p>
<pre><code class="language-cpp">DirectInput8Create(GetModuleHandle(NULL),
                        DIRECTINPUT_VERSION,
                        IID_IDirectInput8,
                        &amp;reinterpret_cast&lt;void*&amp;&gt;(inputDevice),
                         NULL)
</code></pre>
<p>Das heißt, es wird ein Alias vom void* auf den Zeiger inputDevice, der einen anderen Typ hat, benutzt. Das ist in zweierlei Hinsicht problematisch, weshalb eben das Ganze mit static_cast nicht geht:<br />
1. die strikten Aliasregeln von C++ werden gebrochen (was in jedem Falle undefiniertes Verhalten im Sinne das Standards ist) - damit das trotzdem geht, muss der der Compiler explizit diese Form von Aliasing erlauben und damit zurechtkommen; zu beachten ist, dass nicht der Cast an sich undefiniertes Verhalten hat (Alignment ist ja kein Problem wegen 2.), sondern erst der Zugriff über diesen Zeiger auf inputDevice in der Funktion DirectInput8Create.<br />
2. damit das Ganze irgendwie Sinn ergeben kann, muss die Objektrepräsentation beider Zeigertypen identisch sein; das ist bei den Plattformen, die directx kennen, allerdings der Fall.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1368898</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1368898</guid><dc:creator><![CDATA[camper]]></dc:creator><pubDate>Wed, 19 Sep 2007 21:45:45 GMT</pubDate></item><item><title><![CDATA[Reply to C-Cast vs. C++ Cast on Wed, 19 Sep 2007 22:03:55 GMT]]></title><description><![CDATA[<p>Jetzt mal praktisch gesehen: Ich kann auch einfach den C-Style Cast auf (void**) lassen, oder?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1368906</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1368906</guid><dc:creator><![CDATA[BuffCaster]]></dc:creator><pubDate>Wed, 19 Sep 2007 22:03:55 GMT</pubDate></item><item><title><![CDATA[Reply to C-Cast vs. C++ Cast on Wed, 19 Sep 2007 23:15:09 GMT]]></title><description><![CDATA[<p>BuffCaster schrieb:</p>
<blockquote>
<p>Jetzt mal praktisch gesehen: Ich kann auch einfach den C-Style Cast auf (void**) lassen, oder?</p>
</blockquote>
<p>Auch wenn ich gleich Haue krieg: Ja <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/1368917</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1368917</guid><dc:creator><![CDATA[Badestrand]]></dc:creator><pubDate>Wed, 19 Sep 2007 23:15:09 GMT</pubDate></item><item><title><![CDATA[Reply to C-Cast vs. C++ Cast on Thu, 20 Sep 2007 12:27:42 GMT]]></title><description><![CDATA[<p>Badestrand schrieb:</p>
<blockquote>
<p>BuffCaster schrieb:</p>
<blockquote>
<p>Jetzt mal praktisch gesehen: Ich kann auch einfach den C-Style Cast auf (void**) lassen, oder?</p>
</blockquote>
<p>Auch wenn ich gleich Haue krieg: Ja <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>
</blockquote>
<p>Haue bekommst nicht gleich, aber man sollte C-Casts genauso in einer C++-Funktion verwenden wie man jede andere C-Funktion einer aequivalenten C++-Funktion vorziehen sollte: gar nicht. Sicher kannst es machen, es wird auch (leider) noch in den meisten Faellen gemacht, weils weniger zu schreiben ist und den Code nicht so ueberfuellt.<br />
Als kleine Lektuere kann man sich ja mal hier umschauen: <a href="http://www.cplusplus.com/doc/tutorial/typecasting.html" rel="nofollow">http://www.cplusplus.com/doc/tutorial/typecasting.html</a></p>
<p>reinterpret_cast ist genauso ein Hammer wie der C-Cast, static_cast und dynamic_cast sind daher sinnvoll, da sie eine gewisse Form der Sicherheitsueberpruefung liefern. Ist also nur eine Frage der Codingstyles. Verboten ists nicht.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1369252</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1369252</guid><dc:creator><![CDATA[Phil@Uni]]></dc:creator><pubDate>Thu, 20 Sep 2007 12:27:42 GMT</pubDate></item></channel></rss>