<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Dynamic_cast on Cheat Sheet</title><link>/tags/dynamic_cast/</link><description>Recent content in Dynamic_cast on Cheat Sheet</description><generator>Hugo</generator><language>en</language><atom:link href="/tags/dynamic_cast/index.xml" rel="self" type="application/rss+xml"/><item><title>Type Conversions and Casts</title><link>/programming/cpp/primer/type-conversions-and-casts/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>/programming/cpp/primer/type-conversions-and-casts/</guid><description>&lt;h1 id="type-conversions-and-casts"&gt;Type Conversions and Casts&lt;a class="anchor" href="#type-conversions-and-casts"&gt;#&lt;/a&gt;&lt;/h1&gt;
&lt;p&gt;Based on &lt;em&gt;C++ Primer, 5th ed.&lt;/em&gt; Section 4.11 (Type Conversions).&lt;/p&gt;
&lt;h2 id="named-cast-syntax"&gt;Named Cast Syntax&lt;a class="anchor" href="#named-cast-syntax"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;C++ introduces four explicit named casts:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="background-color:#f7f7f7;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-cpp" data-lang="cpp"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;CASTNAME&lt;span style="color:#0550ae"&gt;&amp;lt;&lt;/span&gt;TYPE&lt;span style="color:#0550ae"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#1f2328"&gt;(&lt;/span&gt;EXPRESSION&lt;span style="color:#1f2328"&gt;);&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;CASTNAME&lt;/code&gt;:&lt;/strong&gt; One of &lt;code&gt;static_cast&lt;/code&gt;, &lt;code&gt;const_cast&lt;/code&gt;, &lt;code&gt;reinterpret_cast&lt;/code&gt;, or &lt;code&gt;dynamic_cast&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Value Category Rule:&lt;/strong&gt; If &lt;code&gt;TYPE&lt;/code&gt; is a reference, the result of the cast is an &lt;strong&gt;lvalue&lt;/strong&gt;; otherwise, the result is an &lt;strong&gt;rvalue&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id="the-four-named-casts-at-a-glance"&gt;The Four Named Casts at a Glance&lt;a class="anchor" href="#the-four-named-casts-at-a-glance"&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;table&gt;
 &lt;thead&gt;
 &lt;tr&gt;
 &lt;th style="text-align: left"&gt;Cast&lt;/th&gt;
 &lt;th style="text-align: left"&gt;Timing&lt;/th&gt;
 &lt;th style="text-align: left"&gt;Target / Purpose&lt;/th&gt;
 &lt;th style="text-align: left"&gt;Safety Check&lt;/th&gt;
 &lt;/tr&gt;
 &lt;/thead&gt;
 &lt;tbody&gt;
 &lt;tr&gt;
 &lt;td style="text-align: left"&gt;&lt;strong&gt;&lt;code&gt;static_cast&lt;/code&gt;&lt;/strong&gt;&lt;/td&gt;
 &lt;td style="text-align: left"&gt;Compile-time&lt;/td&gt;
 &lt;td style="text-align: left"&gt;Well-defined conversions (arithmetic, &lt;code&gt;void*&lt;/code&gt; recovery, upcasting)&lt;/td&gt;
 &lt;td style="text-align: left"&gt;Validates convertible types at compile time&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td style="text-align: left"&gt;&lt;strong&gt;&lt;code&gt;const_cast&lt;/code&gt;&lt;/strong&gt;&lt;/td&gt;
 &lt;td style="text-align: left"&gt;Compile-time&lt;/td&gt;
 &lt;td style="text-align: left"&gt;Adds or casts away low-level &lt;code&gt;const&lt;/code&gt; / &lt;code&gt;volatile&lt;/code&gt;&lt;/td&gt;
 &lt;td style="text-align: left"&gt;Compiler ensures types match except for constness&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td style="text-align: left"&gt;&lt;strong&gt;&lt;code&gt;reinterpret_cast&lt;/code&gt;&lt;/strong&gt;&lt;/td&gt;
 &lt;td style="text-align: left"&gt;Compile-time&lt;/td&gt;
 &lt;td style="text-align: left"&gt;Low-level bit pattern reinterpretation&lt;/td&gt;
 &lt;td style="text-align: left"&gt;No safety check (platform-dependent, aliasing risks)&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td style="text-align: left"&gt;&lt;strong&gt;&lt;code&gt;dynamic_cast&lt;/code&gt;&lt;/strong&gt;&lt;/td&gt;
 &lt;td style="text-align: left"&gt;Runtime&lt;/td&gt;
 &lt;td style="text-align: left"&gt;Safe Downcasting &amp;amp; Crosscasting in polymorphic hierarchies&lt;/td&gt;
 &lt;td style="text-align: left"&gt;Queries RTTI (Run-Time Type Identification) via &lt;code&gt;vptr&lt;/code&gt; → &lt;code&gt;vtable&lt;/code&gt;&lt;/td&gt;
 &lt;/tr&gt;
 &lt;/tbody&gt;
&lt;/table&gt;
&lt;hr&gt;
&lt;h2 id="1-static_cast"&gt;1. &lt;code&gt;static_cast&lt;/code&gt;&lt;a class="anchor" href="#1-static_cast"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Used for any well-defined type conversion that does not involve casting away &lt;code&gt;const&lt;/code&gt;.&lt;/p&gt;</description></item><item><title>Object-Oriented Programming (OOP)</title><link>/programming/cpp/primer/oop/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>/programming/cpp/primer/oop/</guid><description>&lt;h1 id="object-oriented-programming-oop"&gt;Object-Oriented Programming (OOP)&lt;a class="anchor" href="#object-oriented-programming-oop"&gt;#&lt;/a&gt;&lt;/h1&gt;
&lt;p&gt;Based on &lt;em&gt;C++ Primer, 5th ed.&lt;/em&gt;, Chapter 15: Object-Oriented Programming and Section 19.2.1: &lt;code&gt;dynamic_cast&lt;/code&gt;, this post summarizes the three pillars of OOP, &lt;strong&gt;Upcast / Downcast, Object Slicing, and Polymorphic Containers&lt;/strong&gt;, &lt;strong&gt;Virtual Functions&lt;/strong&gt; (&lt;code&gt;virtual&lt;/code&gt;, &lt;code&gt;override&lt;/code&gt;, &lt;code&gt;final&lt;/code&gt;), &lt;strong&gt;Pure Virtual Functions&lt;/strong&gt; (&lt;code&gt;= 0&lt;/code&gt;) and &lt;strong&gt;Abstract Base Classes&lt;/strong&gt;, and access control under inheritance.&lt;/p&gt;
&lt;h2 id="1-the-three-pillars-of-oop"&gt;1. The Three Pillars of OOP&lt;a class="anchor" href="#1-the-three-pillars-of-oop"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Object-oriented programming is built on three core principles:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Data Abstraction:&lt;/strong&gt; Define classes that separate interface from implementation. — see &lt;a href="../../programming/cpp/primer/classes/"&gt;Classes&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Inheritance:&lt;/strong&gt; Define classes that model the relationships among similar types.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Dynamic Binding:&lt;/strong&gt; Use objects of derived types while ignoring the details of how they differ.&lt;/li&gt;
&lt;/ul&gt;
&lt;hr&gt;
&lt;h2 id="2-upcast-vs-downcast-and-static-vs-dynamic-type"&gt;2. Upcast vs. Downcast and Static vs. Dynamic Type&lt;a class="anchor" href="#2-upcast-vs-downcast-and-static-vs-dynamic-type"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Expressions dealing with polymorphic types distinguish between two types:&lt;/p&gt;</description></item></channel></rss>