title: Windbg Type Casting
body: <p>I would like to type cast an object of a child class to a parent class in WinDbg at the command window.</p>
  
  <p>Example classes</p>
  
  <pre><code>class parent
  {
  public:
      int a;
      int b;
      parent(){ a = 10; b = 10; }
      parent(int c) : a(a){}
  };
  
  class child : public parent
  {
  public:
      int a;
      int b;
      child(){ a = 20; b = 20; }
      child(int d) : b(d){}
  };
  </code></pre>
  
  <p>I am using Windbg and I was reading through the help file. It shows under C++ Numbers and Operators that I can do the following typecastings from the WinDbg command window:</p>
  
  <pre><code>dynamic_cast &lt;type&gt;(Value) 
  static_cast &lt;type&gt;(Value) 
  reinterpret_cast &lt;type&gt;(Value) 
  const_cast &lt;type&gt;(Value)
  (type) Value
  </code></pre>
  
  <p>So I would type in the Windbg command window:</p>
  
  <pre><code>?? (type) Value
  </code></pre>
  
  <p>What works is</p>
  
  <pre><code>?? (char)a
  ?? static_cast&lt;char&gt;(a)
  </code></pre>
  
  <p>where <em>a</em> is an int.</p>
  
  <p>What doesn't work is</p>
  
  <pre><code>?? (parent)chld
  ?? static_cast&lt;parent&gt;(chld)
  ?? static_cast&lt;mod!parent&gt;(chld)
  </code></pre>
  
  <p>where <em>chld</em> is an object of class child and child inherits class parent.</p>
  
  <p>object example:</p>
  
  <pre><code>child chld;
  </code></pre>
  
  <p>The error returned is </p>
  
  <blockquote>
    <p>Type conflict error at '&lt;EOL&gt;'</p>
  </blockquote>
  
  <p>If I do a <code>x mod!*</code> I get a giant list, in that list are</p>
  
  <pre><code>MOD!parent
  MOD!child
  </code></pre>
  
  <p>If I do a <code>?? chld</code> then the object gets dumped to the screen just fine.</p>
  
  <p>Why I would want to do this? Well you can do </p>
  
  <p><code>?? chld.childattr++</code> and so I would like to actually do <code>?? ((parent)chld).parentattr++</code></p>
  
  <p>The windbg help says:</p>
  
  <blockquote>
    <p>Symbols in C++ Expressions</p>
    
    <p>In a C++ expression, each symbol is interpreted according to its type. Depending on what the symbol refers to, it might be interpreted as an integer, a data structure, a function pointer, <strong>or any other data type</strong>. If you use a symbol that does not correspond to a C++ data type (such as an unmodified module name) within a C++ expression, a syntax error occurs.</p>
  </blockquote>
  
  <p>So I see no reason I shouldn't be able to type cast an object to a parent data type.</p>
  
  <p>I did a lot of searching and nothing really came up for this, if someone could point me in the right direction so that I could read about why this should or shouldn't work or maybe what I need to do to be successful or even why this is not something I should expect from WinDbg.</p>
  
  <p>Edited: To add code example.</p>