<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Coding Friends &#187; casting</title>
	<atom:link href="http://www.codingfriends.com/index.php/tag/casting/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.codingfriends.com</link>
	<description>Coding Friends, place for developers.</description>
	<lastBuildDate>Mon, 06 Sep 2010 20:18:07 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>dynamic_cast &#8211; cpp</title>
		<link>http://www.codingfriends.com/index.php/2010/02/08/dynamic_cast-cpp/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=dynamic_cast-cpp</link>
		<comments>http://www.codingfriends.com/index.php/2010/02/08/dynamic_cast-cpp/#comments</comments>
		<pubDate>Mon, 08 Feb 2010 10:50:35 +0000</pubDate>
		<dc:creator>genux</dc:creator>
				<category><![CDATA[C / C++]]></category>
		<category><![CDATA[casting]]></category>
		<category><![CDATA[dynamic_cast]]></category>

		<guid isPermaLink="false">http://www.codingfriends.com/?p=667</guid>
		<description><![CDATA[The casting of variables, is when you change one type of variable to another, for example to change a integer value to a double value. But when it comes to classes, casting is allot more fun. Because since a casting variable has to be compatible with the return casting type, the base class will need [...]]]></description>
			<content:encoded><![CDATA[<p>The casting of variables, is when you change one type of variable to another, for example to change a integer value to a double value.  But when it comes to classes, casting is allot more fun.  Because since a casting variable has to be compatible with the return casting type, the base class will need to be polymorphic for this to work (<a href="http://www.codingfriends.com/index.php/2010/02/02/polymorphism/">virtual functions</a> within the base class).</p>
<p>The basis of a dynamic_cast syntax is as follows.</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">return</span> variable <span style="color: #000080;">=</span> <span style="color: #0000ff;">dynamic_cast</span><span style="color: #000080;">&lt;</span><span style="color: #0000ff;">return</span> type<span style="color: #000080;">&gt;</span><span style="color: #008000;">&#40;</span>casting value<span style="color: #008000;">&#41;</span></pre></div></div>

<p>To try and demonstrate how dynamic_cast(ing) works here is a example code broken down into chunks.  Here is the base class, that has the virtual int returnX() method.</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">class</span> baseClassDoesWork
<span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">protected</span> <span style="color: #008080;">:</span> 
     <span style="color: #0000ff;">int</span> x<span style="color: #008080;">;</span>
  <span style="color: #0000ff;">public</span> <span style="color: #008080;">:</span> 
     baseClassDoesWork<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>  <span style="color: #008000;">&#123;</span> x <span style="color: #000080;">=</span> <span style="color: #0000dd;">55</span><span style="color: #008080;">;</span> <span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
     <span style="color: #0000ff;">virtual</span> <span style="color: #0000ff;">int</span> returnX<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Base class&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span> <span style="color: #0000ff;">return</span> x<span style="color: #008080;">;</span><span style="color: #008000;">&#125;</span>
     <span style="color: #0000ff;">void</span> setX<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> newXValue<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> x <span style="color: #000080;">=</span> newXValue<span style="color: #008080;">;</span><span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></pre></div></div>

<p>and here is the sub class that will inherit the base class and re-do the virtual method</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">class</span> subBaseClassDoesWork <span style="color: #008080;">:</span> <span style="color: #0000ff;">public</span> baseClassDoesWork
<span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">public</span> <span style="color: #008080;">:</span> 
      subBaseClassDoesWork<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008080;">:</span> baseClassDoesWork<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span><span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
      <span style="color: #0000ff;">int</span> returnX<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Subclass&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span> <span style="color: #0000ff;">return</span> x<span style="color: #008080;">;</span><span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></pre></div></div>

<p>here, there is  no real difference between the two functions, because the demo is about how dynamic casting works, but I have added in some debugging code to say what class is being called.</p>
<p>Since a dynamic_cast will cast the virtual functions, then the polymorphism effects the casting and newer virtual function that is in the subclass still will call called.  But the casting to the class other functions will not &#8220;follow&#8221; the cast.  In the example below first we are creating a subclass and then creating a base class from a dynamic_cast of the subclass (as stated before the base class casting will still include the virtual function from the subclass), so that if you want to convert to another class that also inherited from the base class but not the subclass then you can now convert to the new class.</p>
<p>Here is the code.</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#include &lt;iostream&gt;</span>
&nbsp;
<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> std<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">class</span> baseClassDoesWork
<span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">protected</span> <span style="color: #008080;">:</span> 
     <span style="color: #0000ff;">int</span> x<span style="color: #008080;">;</span>
  <span style="color: #0000ff;">public</span> <span style="color: #008080;">:</span> 
     baseClassDoesWork<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>  <span style="color: #008000;">&#123;</span> x <span style="color: #000080;">=</span> <span style="color: #0000dd;">55</span><span style="color: #008080;">;</span> <span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
     <span style="color: #0000ff;">virtual</span> <span style="color: #0000ff;">int</span> returnX<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Base class&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span> <span style="color: #0000ff;">return</span> x<span style="color: #008080;">;</span><span style="color: #008000;">&#125;</span>
     <span style="color: #0000ff;">void</span> setX<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> newXValue<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> x <span style="color: #000080;">=</span> newXValue<span style="color: #008080;">;</span><span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">class</span> subBaseClassDoesWork <span style="color: #008080;">:</span> <span style="color: #0000ff;">public</span> baseClassDoesWork
<span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">public</span> <span style="color: #008080;">:</span> 
      subBaseClassDoesWork<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008080;">:</span> baseClassDoesWork<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span><span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
      <span style="color: #0000ff;">int</span> returnX<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Subclass&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span> <span style="color: #0000ff;">return</span> x<span style="color: #008080;">;</span><span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #666666;">// this one does work because of the virtual method in the base class.</span>
    <span style="color: #666666;">// but we can create a sub class of the base class first.</span>
    subBaseClassDoesWork <span style="color: #000040;">*</span>subBase <span style="color: #000080;">=</span> <span style="color: #0000dd;">new</span> subBaseClassDoesWork<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #666666;">// convert it to the base class</span>
    baseClassDoesWork <span style="color: #000040;">*</span>baseDoes <span style="color: #000080;">=</span> <span style="color: #0000ff;">dynamic_cast</span><span style="color: #000080;">&lt;</span>baseClassDoesWork<span style="color: #000040;">*</span><span style="color: #000080;">&gt;</span><span style="color: #008000;">&#40;</span>subBase<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #666666;">// check to see if the casting worked e.g. not equal to 0</span>
    <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>baseDoes <span style="color: #000080;">==</span> <span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span>
      <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Bad casting&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
    <span style="color: #0000ff;">else</span>
      <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> baseDoes<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>returnX<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #666666;">// re-set the value of X</span>
    baseDoes<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>setX<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">30</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #666666;">// convert it back again to the sub class and things are still great :).</span>
    subBaseClassDoesWork <span style="color: #000040;">*</span>subBase2 <span style="color: #000080;">=</span> <span style="color: #0000ff;">dynamic_cast</span><span style="color: #000080;">&lt;</span>subBaseClassDoesWork<span style="color: #000040;">*</span><span style="color: #000080;">&gt;</span><span style="color: #008000;">&#40;</span>baseDoes<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>subBase2 <span style="color: #000080;">==</span> <span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span>
      <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Bad casting&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
    <span style="color: #0000ff;">else</span>
      <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> subBase2<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>returnX<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>The output would be</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">Subclass
<span style="color: #000000;">55</span>
Subclass
<span style="color: #000000;">30</span></pre></div></div>

<p>Where as this would not work because there is no virtual functions to really link the classes together (polymorphic), error included in the source where the error would be.</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#include &lt;iostream&gt;</span>
&nbsp;
<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> std<span style="color: #008080;">;</span>
&nbsp;
 <span style="color: #0000ff;">class</span> baseClassNotWork
 <span style="color: #008000;">&#123;</span>
   <span style="color: #0000ff;">protected</span> <span style="color: #008080;">:</span> 
      <span style="color: #0000ff;">int</span> x<span style="color: #008080;">;</span>
   <span style="color: #0000ff;">public</span> <span style="color: #008080;">:</span> 
      baseClassNotWork<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>  <span style="color: #008000;">&#123;</span> x <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> <span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
      <span style="color: #0000ff;">int</span> returnX<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> <span style="color: #0000ff;">return</span> x<span style="color: #008080;">;</span><span style="color: #008000;">&#125;</span>
 <span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
 <span style="color: #0000ff;">class</span> subBaseClassNotWork <span style="color: #008080;">:</span> <span style="color: #0000ff;">public</span> baseClassNotWork
 <span style="color: #008000;">&#123;</span>
   <span style="color: #0000ff;">public</span> <span style="color: #008080;">:</span> 
       subBaseClassNotWork<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008080;">:</span> baseClassNotWork<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span><span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
       <span style="color: #0000ff;">int</span> returnX<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> <span style="color: #0000ff;">return</span> x<span style="color: #008080;">;</span><span style="color: #008000;">&#125;</span>
 <span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
     baseClassNotWork <span style="color: #000040;">*</span>baseNot <span style="color: #000080;">=</span> <span style="color: #0000dd;">new</span> baseClassNotWork<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
     <span style="color: #666666;">//error: cannot dynamic_cast ‘baseNot’ (of type ‘class baseClassNotWork*’) to type ‘class subBaseClassNotWork*’ (source type is not polymorphic)</span>
     subBaseClassNotWork <span style="color: #000040;">*</span>subBaseNot <span style="color: #000080;">=</span> <span style="color: #0000ff;">dynamic_cast</span><span style="color: #000080;">&lt;</span>subBaseClassNotWork<span style="color: #000040;">*</span><span style="color: #000080;">&gt;</span><span style="color: #008000;">&#40;</span>baseNot<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.codingfriends.com/index.php/2010/02/08/dynamic_cast-cpp/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
