C++ type casting with example for C developers
Tags: cpp, tutorial, c, computerscience
Published: 2019-10-13 13:36:07

<!-- wp:paragraph -->
<p>The typecasting is the feature which makes C++ more type-safe, robust & may convince you to use it over C. But this is also a more underrated topic when you are a newbie or moving from C background. Hence, I come up with an article on it . Here, we will not only see the C++ typecasting but we will also cover why we need it & cheat codes for C developers to remember & employ it easily. Although I am not an expert but this is what I have learned so far from various sources & 5+ yrs of industry experience. </p>
<!-- /wp:paragraph -->

> /!\\\: This article has been originally published on my blog. If you are interested in receiving my latest articles, [please sign up to my newsletter](http://eepurl.com/gDNybv).

<!-- wp:paragraph -->
<p>In C++, there are 5 different types of casts: C-style casts, <code>static_cast</code>, <code>const_cast</code>, <code>dynamic_cast</code>, and <code>reinterpret_cast</code>.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>I usually start with "Why we need it?", but this time first we quickly go through some jargons & I will end this article with some of CPP core guidelines.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h2>Jargons you need to face</h2>
<!-- /wp:heading -->

<!-- wp:list {"ordered":true} -->
<ol><li><strong>Implicit conversion:</strong> where the compiler automatically typecast. Like <code>float f = 3;</code>, here compiler will not complain but directly transform <code>3</code> which is of type integer into <code>float</code> &amp; assign to <code>f</code>.</li><li><strong>Explicit conversions</strong>: where the developer uses a casting operator to direct the conversion. All types of manual casting fall under the explicit type conversions category. Like <code>int * p = (int*)std::malloc(10);</code>, here we explicitly casting <code>void*</code> to <code>int*</code>.</li><li><strong><code>l-value</code></strong>: an identifier which represents memory location. For example, variable name, <code>*ptr</code> where <code>ptr</code> points to a memory location, etc.</li><li><strong><code>r-value</code></strong>: a value which is not <code>l-value</code>,  <code>r-value</code> appear on the right-hand side of assignment(<code>=</code>) operator. Like</li></ol>
<!-- /wp:list -->

<!-- wp:enlighter/codeblock {"language":"cpp"} -->
<pre class="EnlighterJSRAW" data-enlighter-language="cpp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">int a = 5; // 5 = r-value, 
q = p + 5; // p + 5 is r-value</pre>
<!-- /wp:enlighter/codeblock -->

<!-- wp:paragraph -->
<p>Note: Although there are some exceptions &amp; more to learn on <code>l-value</code> &amp; <code>r-value</code> which I have discussed <a rel="noreferrer noopener" aria-label="here (opens in a new tab)" href="http://www.vishalchovatiya.com/lvalue-rvalue-and-their-references-with-example-in-cpp/" target="_blank"><span style="color:#1322ee" class="color">here</span></a>. </p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h2>Why we need typecasting?</h2>
<!-- /wp:heading -->

<!-- wp:list -->
<ul><li>Data is a representation of the bits(<code>0</code>s &amp; <code>1</code>s) in memory.</li><li>Data-type is compiler directive which tells the compiler how to store &amp; process particular data.</li><li><code>uint32_t a = 5;</code> by this statement you can presume that 4 bytes will be reserved in your memory &amp; upon execution, it will store <code>0000 0000 0000 0000 0000 0000 0000 0101</code> data bits in that memory location. This was plain &amp; simple. </li><li>Let's go a bit further, <code>float f = 3.0;</code> this statement will also reserve 4 bytes in memory &amp; store data bits in form of 1). the sign bit, 2). exponent &amp; 3). mantissa. Recall <a aria-label="how float stored in memory (opens in a new tab)" rel="noreferrer noopener" href="http://www.vishalchovatiya.com/how-floating-point-no-is-stored-memory/" target="_blank"><span style="color:#1714f2" class="color">how floating-point numbers are stored in memory</span></a>.</li><li>But when you write like <code>float f = 3;</code>, the compiler will be confused that how to store an integer value in float type of memory. </li><li>So it will automatically presume(Implicit conversion here) that you want to store <code>3.0</code> rather than <code>3</code> which is technically same from the human point of view but it's different when you think from <a rel="noreferrer noopener" aria-label="computer memory perspective (opens in a new tab)" href="http://www.vishalchovatiya.com/memory-layout-of-cpp-object/" target="_blank"><span style="color:#1b17f9" class="color">computer memory perspective</span></a> cause they stored differently.</li><li>There are many such scenarios where you provide data to store in memory which used to represent different data type.</li><li>For example, in the following example, you are trying to assign an object of type <code>B</code> into an object of type <code>A</code></li></ul>
<!-- /wp:list -->

<!-- wp:enlighter/codeblock {"language":"cpp"} -->
<pre class="EnlighterJSRAW" data-enlighter-language="cpp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">class A{};
class B{};

int main ()
{
  B b;
  A a = b; 
  return 0;
}</pre>
<!-- /wp:enlighter/codeblock -->

<!-- wp:list -->
<ul><li>In such scenario compiler can not presume anything &amp; simply throws a compilation error:</li></ul>
<!-- /wp:list -->

<!-- wp:enlighter/codeblock {"language":"cpp"} -->
<pre class="EnlighterJSRAW" data-enlighter-language="cpp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">exit status 1
error: no viable conversion from 'B' to 'A'
  A a = b;
    ^   ~
note: candidate constructor (the implicit copy constructor) not viable: no known conversion from 'B' to 'const A &amp;' for 1st argument
class A{};
      ^
note: candidate constructor (the implicit move constructor) not viable: no known conversion from 'B' to 'A &amp;&amp;' for 1st argument
class A{};
      ^
1 error generated.</pre>
<!-- /wp:enlighter/codeblock -->

<!-- wp:list -->
<ul><li>But when you define a conversion operator as follows:</li></ul>
<!-- /wp:list -->

<!-- wp:enlighter/codeblock {"language":"cpp"} -->
<pre class="EnlighterJSRAW" data-enlighter-language="cpp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">class B {
public:
  operator A(){
    cout&lt;&lt;"CONVERSION OPERATOR\n";
    return A();
  } 
};</pre>
<!-- /wp:enlighter/codeblock -->

<!-- wp:list -->
<ul><li>The compiler will simply call this member function &amp; won't throw any error because programmer explicitly mentioning that this is how he/she wants to convert.</li></ul>
<!-- /wp:list -->

<!-- wp:heading {"level":3} -->
<h2>5️⃣ C++ type casting with example for C developers</h2>
<!-- /wp:heading -->


<!-- wp:heading {"level":3} -->
<h2><code>C-style casts</code></h2>
<!-- /wp:heading -->

<!-- wp:enlighter/codeblock {"language":"cpp"} -->
<pre class="EnlighterJSRAW" data-enlighter-language="cpp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">int main() { 
    float res = 10 / 4;
    cout&lt;&lt;res&lt;&lt;endl;
    return 0; 
}</pre>
<!-- /wp:enlighter/codeblock -->

<!-- wp:list -->
<ul><li>When you will try to run the above code, you will get <code>2</code> as output which we didn't expect. To initialize <code>res</code> variable correctly we need to typecast using float as follows:</li></ul>
<!-- /wp:list -->

<!-- wp:enlighter/codeblock {"language":"cpp"} -->
<pre class="EnlighterJSRAW" data-enlighter-language="cpp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">float res = (float)10 / 4;</pre>
<!-- /wp:enlighter/codeblock -->

<!-- wp:list -->
<ul><li>Now your answer will be <code>2.5</code>. This type of casting is very simple &amp; straight forward as it appears.</li><li>You can also write above casting in C++ as:</li></ul>
<!-- /wp:list -->

<!-- wp:enlighter/codeblock {"language":"cpp"} -->
<pre class="EnlighterJSRAW" data-enlighter-language="cpp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">float res = float(10) / 4;</pre>
<!-- /wp:enlighter/codeblock -->

<!-- wp:list -->
<ul><li>C-style casts can change a data type without changing the underlying memory representation which may lead to garbage results. </li></ul>
<!-- /wp:list -->


<!-- wp:heading {"level":3} -->
<h2><code>static_cast</code> </h2>
<!-- /wp:heading -->

<!-- wp:list -->
<ul><li>If you are C developer like me, then this will be your best goto C++ cast which fits in most of the example like:</li></ul>
<!-- /wp:list -->

<!-- wp:enlighter/codeblock {"language":"cpp"} -->
<pre class="EnlighterJSRAW" data-enlighter-language="cpp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">int * p = std::malloc(10);</pre>
<!-- /wp:enlighter/codeblock -->

<!-- wp:list -->
<ul><li>When you try to compile above code using C compiler it works fine. But C++ compiler is not kind enough. It will throw an error as follows :</li></ul>
<!-- /wp:list -->

<!-- wp:enlighter/codeblock {"language":"cpp"} -->
<pre class="EnlighterJSRAW" data-enlighter-language="cpp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">exit status 1
error: cannot initialize a variable of type 'int *' with an rvalue of type 'void *'
  int * p = std::malloc(10);
        ^   ~~~~~~~~~~
1 error generated.</pre>
<!-- /wp:enlighter/codeblock -->

<!-- wp:list -->
<ul><li>The first thing that comes to your mind is the C-style cast:</li></ul>
<!-- /wp:list -->

<!-- wp:enlighter/codeblock {"language":"cpp"} -->
<pre class="EnlighterJSRAW" data-enlighter-language="cpp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">int * p = (int*)std::malloc(10);</pre>
<!-- /wp:enlighter/codeblock -->

<!-- wp:list -->
<ul><li>This will work, but this style of the cast is not recommended in C++. <code>static_cast</code> handles implicit conversions like this. We will primarily use it for converting in places where implicit conversions fail, such as std::malloc.</li></ul>
<!-- /wp:list -->

<!-- wp:enlighter/codeblock {"language":"cpp"} -->
<pre class="EnlighterJSRAW" data-enlighter-language="cpp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">int * p = static_cast&lt;int*>(std::malloc(10));</pre>
<!-- /wp:enlighter/codeblock -->

<!-- wp:list -->
<ul><li>The main advantage of <code>static_cast</code> is that it provides compile-time type checking, making it harder to make an inadvertent error. Let's understand this with C++ example:</li></ul>
<!-- /wp:list -->

<!-- wp:enlighter/codeblock {"language":"cpp"} -->
<pre class="EnlighterJSRAW" data-enlighter-language="cpp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">class B {};
class D : public B {};
class X {};

int main()
{
  D* d = new D;
  B* b = static_cast&lt;B*>(d); // this works
  X* x = static_cast&lt;X*>(d); // ERROR - Won't compile
  return 0;
}</pre>
<!-- /wp:enlighter/codeblock -->

<!-- wp:list -->
<ul><li>As you can see, there is no easy way to distinguish between the two situations without knowing a lot about all the classes involved.</li><li>Another problem with the C-style casts is that it is too hard to locate. In complex expressions, it can be very hard to see C-style casts e.g. <code>T(something)</code> syntax is equivalent to <code>(T)something</code>.</li></ul>
<!-- /wp:list -->


<!-- wp:heading {"level":3} -->
<h2><code>const_cast</code> </h2>
<!-- /wp:heading -->

<!-- wp:list -->
<ul><li>Now we will directly jump to example. No theory> can explain this better than example. </li></ul>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p><strong>1. Ignore constness </strong></p>
<!-- /wp:paragraph -->

<!-- wp:enlighter/codeblock {"language":"cpp"} -->
<pre class="EnlighterJSRAW" data-enlighter-language="cpp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">int i = 0;
const int&amp; ref = i;
const int* ptr = &i;

*ptr = 3; // Not OK
const_cast&lt;int&amp;>(ref) = 3;  //OK
*const_cast&lt;int*>(ptr) = 3; //OK</pre>
<!-- /wp:enlighter/codeblock -->

<!-- wp:list -->
<ul><li>You are allowed to modify <code>i</code>, because of the object(<code>i</code> here) being assigned to, is not <code>const</code>. If you add const qualifier to <code>i</code>, code will compile,  but its behaviour will be undefined (which can mean anything from "it works just fine" to "the program will crash>".) </li></ul>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p><strong>2. Modifying data member using <code>const</code> <code>this</code> pointer</strong></p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><li><code>const_cast</code> can be used to change non-const class members by a method in which this pointer declared as const. - This can also be useful when overloading member functions based on <code>const</code>, for instance:</li></ul>
<!-- /wp:list -->

<!-- wp:enlighter/codeblock {"language":"cpp"} -->
<pre class="EnlighterJSRAW" data-enlighter-language="cpp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">class X
{
public:
    int var;
    void changeAndPrint(int temp) const
    {
        this->var = temp;                    // Throw compilation error
        (const_cast&lt;X *>(this))->var = temp; // Works fine
    }
    void changeAndPrint(int *temp)
    {
        // Do some stuff
    }
};
int main()
{
    int a = 4;
    X x;
    x.changeAndPrint(&amp;a);
    x.changeAndPrint(5);
    cout &lt;&lt; x.var &lt;&lt; endl;
    return 0;
}</pre>
<!-- /wp:enlighter/codeblock -->

<!-- wp:paragraph -->
<p><strong>3. Pass <code>const</code> argument to a function which accepts only non-const argument</strong></p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><li><code>const_cast</code> can also be used to pass const data to a function that doesn’t receive const argument. See the following code:</li></ul>
<!-- /wp:list -->

<!-- wp:enlighter/codeblock {"language":"cpp"} -->
<pre class="EnlighterJSRAW" data-enlighter-language="cpp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">int fun(int* ptr) 
{ 
    return (*ptr + 10); 
} 

int main(void) 
{ 
    const int val = 10; 
    cout &lt;&lt; fun(const_cast &lt;int *>(&amp;val)); 
    return 0; 
} </pre>
<!-- /wp:enlighter/codeblock -->

<!-- wp:paragraph -->
<p><strong>4. Castaway <code>volatile</code> attribute</strong></p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><li><code>const_cast</code> can also be used to cast away <code>volatile</code> attribute. Whatever we discussed above in <code>const_cast</code> is also valid for <code>volatile</code> keyword.</li></ul>
<!-- /wp:list -->


<!-- wp:heading {"level":3} -->
<h2><code>dynamic_cast</code> </h2>
<!-- /wp:heading -->

<!-- wp:list -->
<ul><li><code>dynamic_cast</code> uses the type checking at runtime in contrary to <code>static_cast</code> which does it at compile time. <code>dynamic_cast</code> is more useful when you don't know the type of input which it represents. Let assume:</li></ul>
<!-- /wp:list -->

<!-- wp:enlighter/codeblock {"language":"cpp"} -->
<pre class="EnlighterJSRAW" data-enlighter-language="cpp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">Base* CreateRandom()
{
    if( (rand()%2) == 0 )
        return new Derived1;
    else
        return new Derived2;
}

Base* base = CreateRandom();</pre>
<!-- /wp:enlighter/codeblock -->

<!-- wp:list -->
<ul><li>As you can see, we don't know which object will be returned by <code>CreateRandom()</code> at run time but you want to execute <code>Method1()</code> of <code>Derived1</code> if it returns <code>Derived1</code>. So in this scenario, you can use <code>dynamic_cast</code> as follows</li></ul>
<!-- /wp:list -->

<!-- wp:enlighter/codeblock {"language":"cpp"} -->
<pre class="EnlighterJSRAW" data-enlighter-language="cpp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">Derived1 *pD1 = dynamic_cast&lt;Derived1 *>(base);
if (pD1){
    pD1->Method1();
}</pre>
<!-- /wp:enlighter/codeblock -->

<!-- wp:list -->
<ul><li>In case, if input of <code>dynamic_cast</code> does not point to valid data, it will return <code>nullptr</code> for pointers or throw a <code>std::bad_cast</code> exception for references. In order to work with <code>dynamic_cast</code>, your classes must be polymorphic type i.e. must include at least one virtual methods.</li><li><code>dynamic_cast</code> take advantage of <code>RTTI</code>(<a rel="noreferrer noopener" aria-label="Run Time Type Identification (opens in a new tab)" href="https://en.wikipedia.org/wiki/Run-time_type_information" target="_blank"><span style="color:#1511f9" class="color">Run Time Type Identification</span></a>) mechanism.</li></ul>
<!-- /wp:list -->


<!-- wp:heading {"level":3} -->
<h2><code>reinterpret_cast</code> </h2>
<!-- /wp:heading -->

<!-- wp:list -->
<ul><li><code>reinterpret_cast</code> is a compiler directive which tells the compiler to treat the current type as a new type.</li><li>You can use <code>reinterpret_cast</code> to cast any pointer or integral type to any other pointer or integral type. </li><li>This can lead to dangerous situations: nothing will stop you from converting an <code>int</code> to an <code>std::string*</code>.</li><li>You will use <code>reinterpret_cast</code> in your embedded systems. A common scenario where <code>reinterpret_cast</code> applies is converting between <code>uintptr_t</code> and an actual pointer or between:</li></ul>
<!-- /wp:list -->

<!-- wp:enlighter/codeblock {"language":"cpp"} -->
<pre class="EnlighterJSRAW" data-enlighter-language="cpp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">error: static_cast from 'int *' to 'uintptr_t'
      (aka 'unsigned long') is not allowed
        uintptr_t ptr = static_cast&lt;uintptr_t>(p);
                        ^~~~~~~~~~~~~~~~~~~~~~~~~
1 error generated.</pre>
<!-- /wp:enlighter/codeblock -->

<!-- wp:list -->
<ul><li>Instead, use this:</li></ul>
<!-- /wp:list -->

<!-- wp:enlighter/codeblock {"language":"cpp"} -->
<pre class="EnlighterJSRAW" data-enlighter-language="cpp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">uintptr_t ptr = reinterpret_cast&lt;uintptr_t>(p);</pre>
<!-- /wp:enlighter/codeblock -->


<!-- wp:paragraph -->
<p>I have tried to cover most of the intricacies to clear the main concept behind different typecasting, but still, there might be a chance that I may miss some.  So, this is it for C++ type casting with an example for C developers. Let's quickly recap:</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h2>Cheat code for C developers moving to C++ on type casting</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>After reading all this you may confuse on what to use &amp; when! That's why I have created this cheat code </p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><li>Avoid C-style casts. Be sure about what you want while casting.</li><li>Use <code>static_cast</code> wherever you were using C-style cast.</li><li>Use <code>dynamic_cast</code> with polymorphic classes. Keep in mind that only use <code>dynamic_cast</code> on classes with at least one virtual member in the inheritance hierarchy.</li><li>Use <code>const_cast</code> when you need to remove <code>const</code> or <code>volatile</code> qualifiers. </li><li> Use <code>reinterpret_cast</code> when you have no options. </li></ul>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p>Note: <code>const_cast</code> and <code>reinterpret_cast</code> should generally be avoided because they can be harmful if used incorrectly. Don't use it unless you have a very good reason to use them.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3>Some of C++ core guidelines</h3>
<!-- /wp:heading -->

<!-- wp:list -->
<ul><li><a rel="noreferrer noopener" href="https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#p4-ideally-a-program-should-be-statically-type-safe" target="_blank"><span style="color:#2a18f5" class="color">P.4: Ideally, a program should be statically (compile-time)  type safe</span></a><span style="color:#2a18f5" class="color"> </span></li><li><a rel="noreferrer noopener" aria-label=" (opens in a new tab)" href="https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Res-casts" target="_blank"><span style="color:#2a18f5" class="color">ES.48: Avoid casts</span></a><span style="color:#2a18f5" class="color"><span style="color:#0537ec" class="color"></span></span></li><li><a rel="noreferrer noopener" aria-label=" (opens in a new tab)" href="https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Res-casts-named" target="_blank"><span style="color:#2a18f5" class="color">ES.49: If you must use a cast, use a named cast</span></a><span style="color:#2a18f5" class="color"><span style="color:#0537ec" class="color"></span></span></li><li><a rel="noreferrer noopener" aria-label=" (opens in a new tab)" href="https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Res-casts-const" target="_blank"><span style="color:#2a18f5" class="color">ES.50: Don’t cast away </span></a><code><a rel="noreferrer noopener" aria-label=" (opens in a new tab)" href="https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Res-casts-const" target="_blank"><span style="color:#2a18f5" class="color">const</span></a></code><span style="color:#2a18f5" class="color">  </span></li><li><a rel="noreferrer noopener" aria-label=" (opens in a new tab)" href="https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rh-dynamic_cast" target="_blank"><span style="color:#2a18f5" class="color">C.146: Use </span></a><code><a rel="noreferrer noopener" aria-label=" (opens in a new tab)" href="https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rh-dynamic_cast" target="_blank"><span style="color:#2a18f5" class="color">dynamic_cast</span></a></code><a rel="noreferrer noopener" aria-label=" (opens in a new tab)" href="https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rh-dynamic_cast" target="_blank"><span style="color:#2a18f5" class="color"> where class hierarchy navigation is unavoidable</span></a></li><li><a rel="noreferrer noopener" aria-label=" (opens in a new tab)" href="https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rh-ref-cast" target="_blank"><span style="color:#2a18f5" class="color">C.147: Use </span></a><code><a rel="noreferrer noopener" aria-label=" (opens in a new tab)" href="https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rh-ref-cast" target="_blank"><span style="color:#2a18f5" class="color">dynamic_cast</span></a></code><a rel="noreferrer noopener" aria-label=" (opens in a new tab)" href="https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rh-ref-cast" target="_blank"><span style="color:#2a18f5" class="color"> to a reference type when failure to find the required class is considered an error</span></a></li><li><a rel="noreferrer noopener" aria-label=" (opens in a new tab)" href="https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rh-ptr-cast" target="_blank"><span style="color:#2a18f5" class="color">C.148: Use </span></a><code><a rel="noreferrer noopener" aria-label=" (opens in a new tab)" href="https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rh-ptr-cast" target="_blank"><span style="color:#2a18f5" class="color">dynamic_cast</span></a></code><a rel="noreferrer noopener" aria-label=" (opens in a new tab)" href="https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rh-ptr-cast" target="_blank"><span style="color:#2a18f5" class="color"> to a pointer type when failure to find the required class is considered a valid alternative</span></a><span style="color:#2a18f5" class="color"> </span></li></ul>
<!-- /wp:list -->

## Other related articles

- [21 new features of Modern C++ to use in your project](https://dev.to/visheshpatel/21-new-features-of-modern-c-to-use-in-your-project-3f87)
- [All about lambda functions in C++(from C++11 to C++17)](https://dev.to/visheshpatel/all-about-lambda-function-in-c-from-c-11-to-c-17-3p5h)
- [How floating-point no is stored in memory?](https://dev.to/visheshpatel/how-floating-point-no-is-stored-memory-47od)

[Have any suggestions, query or wants to say `Hi`? Take the Pressure Off, you are just a click away.](http://www.vishalchovatiya.com/contact-2/)🖱️