Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The main reason is that classic C casts make no distinction between what we call <code>static_cast&lt;&gt;()</code>, <code>reinterpret_cast&lt;&gt;()</code>, <code>const_cast&lt;&gt;()</code>, and <code>dynamic_cast&lt;&gt;()</code>. These four things are completely different.</p> <p>A <code>static_cast&lt;&gt;()</code> is usually safe. There is a valid conversion in the language, or an appropriate constructor that makes it possible. The only time it's a bit risky is when you cast down to an inherited class; you must make sure that the object is a actually the descendant that you claim it is, by means external to the language (like a flag in the object). A <code>dynamic_cast&lt;&gt;()</code> is safe as long as the result is checked (pointer) or a possible exception is taken into account (reference). </p> <p>A <code>reinterpret_cast&lt;&gt;()</code> (or a <code>const_cast&lt;&gt;()</code>) on the other hand is always dangerous. You tell the compiler: "trust me: I know this doesn't look like a <code>foo</code> (this looks as if it isn't mutable), but it is". </p> <p>The first problem is that it's almost impossible to tell which one will occur in a C-style cast without looking at large and disperse pieces of code and knowing all the rules.</p> <p>Let's assume these:</p> <pre><code>class CMyClass : public CMyBase {...}; class CMyOtherStuff {...} ; CMyBase *pSomething; // filled somewhere </code></pre> <p>Now, these two are compiled the same way:</p> <pre><code>CMyClass *pMyObject; pMyObject = static_cast&lt;CMyClass*&gt;(pSomething); // Safe; as long as we checked pMyObject = (CMyClass*)(pSomething); // Same as static_cast&lt;&gt; // Safe; as long as we checked // but harder to read </code></pre> <p>However, let's see this almost identical code:</p> <pre><code>CMyOtherStuff *pOther; pOther = static_cast&lt;CMyOtherStuff*&gt;(pSomething); // Compiler error: Can't convert pOther = (CMyOtherStuff*)(pSomething); // No compiler error. // Same as reinterpret_cast&lt;&gt; // and it's wrong!!! </code></pre> <p>As you can see, there is no easy way to distinguish between the two situations without knowing a lot about all the classes involved.</p> <p>The second problem is that the C-style casts are too hard to locate. In complex expressions it can be very hard to see C-style casts. It is virtually impossible to write an automated tool that needs to locate C-style casts (for example a search tool) without a full blown C++ compiler front-end. On the other hand, it's easy to search for "static_cast&lt;" or "reinterpret_cast&lt;".</p> <pre><code>pOther = reinterpret_cast&lt;CMyOtherStuff*&gt;(pSomething); // No compiler error. // but the presence of a reinterpret_cast&lt;&gt; is // like a Siren with Red Flashing Lights in your code. // The mere typing of it should cause you to feel VERY uncomfortable. </code></pre> <p>That means that, not only are C-style casts more dangerous, but it's a lot harder to find them all to make sure that they are correct.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload