Note that there are some explanatory texts on larger screens.

plurals
  1. POC++ Casting Operators and traditional C casting operators
    primarykey
    data
    text
    <blockquote> <p><strong>Possible Duplicate:</strong><br> <a href="https://stackoverflow.com/questions/332030/when-should-static-cast-dynamic-cast-and-reinterpret-cast-be-used">When should static_cast, dynamic_cast and reinterpret_cast be used?</a> </p> </blockquote> <p>I have done a lot of googling to find about:</p> <ol> <li>why to use C++ casting operators over traditional C style casting operators? </li> <li>When to use C++ casting operators, some live examples?</li> </ol> <p>The following is what I found: </p> <ul> <li>Traditionally any C++ casting operators is used for better maintenance of your code (ie) we can easily find where casting is used in code by just searching for this complex notation (reinterpret_cast&lt;) unlike C style casting operators. </li> </ul> <p><em>Now let me brief state why and when for each of the C++ casting operators</em> </p> <p><strong>static_cast:</strong></p> <p>Why use it over C style casting? <code>static_cast</code> is used to perform conversion between related types.</p> <p>Examples : </p> <pre><code> Class A {}; Class B {}; A* a = new A(); B* b = static_cast&lt;B*&gt;(a); // Compiler error B* b1 = (A*)a; // Works fine float f; int addr = (int)(&amp;f); // Works fine int addr = static_cast&lt;int&gt;(&amp;f); // Compiler error </code></pre> <p>But I wanted to know a real use case of when to use above code?</p> <p><strong>reinterpret_cast :</strong></p> <p><code>reinterpret_cast</code> casts pointers to unrelated types.</p> <p>Examples:</p> <pre><code> Class A {}; Class B {}; A* a = new A(); B* b = reinterpret_cast&lt;B*&gt;(a); // Works fine B* b1 = (A*)a; // Works fine float f; int addr = (int)(&amp;f); // Works fine int addr = reinterpret_cast&lt;int&gt;(&amp;f); // Works fine int ai = 10; float af = 13.33; // Would depend on how floating point is stored in machine // int&amp; since reinterpret_cast expects either the type or operand to be pointer or reference int ki = reinterpret_cast&lt;int&amp;&gt;(af); // ki would not be 13 int kitemp = (int)af; // kitemp would be 13 // The same reinterpret_cast behaviour can be achieved using this, int* in = (int*)(af); cout &lt;&lt; (*in); </code></pre> <p>My question is how else is <code>reinterpret_cast</code> different from C style casting? I'm unable to find why to use it over traditional casting operators and also when to use it?</p> <p>Another important example which makes these operators worse is:</p> <pre><code> const unsigned int * p; (int*)p; // Would remove unsigned and const at one shot // Using C++ casting operators // Const_cast expects a pointer or a reference reinterpret_cast&lt;int*&gt;(const_cast&lt;unsigned int* &gt;(p)); </code></pre> <p>Writing the above code to remove <code>const</code> and <code>unsigned</code> is much more complex in C++ casting? Then why do people use <code>reinterpret_cast</code>, <code>const_cast</code> or <code>static_cast</code> over tradional C casting operators?</p> <p>I do understand about <code>dynamic_cast</code> used in case of polymorphic classes; again this operator too has an extra cost of RTTI.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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