title: Checking the object type in C++11
body: <p>I have class B that inherits from A.</p>
  
  <pre><code>class A
  {
  };
  
  class B : public A
  {
  };
  </code></pre>
  
  <p>And I have three objects.</p>
  
  <pre><code>A* a = new A();
  A* a2 = new B();
  B* b = new B();
  </code></pre>
  
  <p>I'd like to if check a is object of type A, a2 is object of type B (not A), and b is object of type B.</p>
  
  <p>I tried typed comparison, but it doesn't give me correct answer. </p>
  
  <pre><code>cout &lt;&lt; (typeid(*a) == typeid(A)) &lt;&lt; endl; // -&gt; 1
  cout &lt;&lt; (typeid(*a2) == typeid(A)) &lt;&lt; endl; // -&gt; 1
  cout &lt;&lt; (typeid(*b) == typeid(A)) &lt;&lt; endl; // -&gt; 0
  
  cout &lt;&lt; (typeid(*a) == typeid(B)) &lt;&lt; endl; // -&gt; 0
  cout &lt;&lt; (typeid(*a2) == typeid(B)) &lt;&lt; endl; // -&gt; 0
  cout &lt;&lt; (typeid(*b) == typeid(B)) &lt;&lt; endl; // -&gt; 1
  </code></pre>
  
  <p>I tried dynamic casting, but I got compile error.</p>
  
  <pre><code>B* derived = dynamic_cast&lt;B*&gt;(a);
  if (derived) {
      cout &lt;&lt; "a is B";
  }
  derived = dynamic_cast&lt;B*&gt;(a2);
  if (derived) {
      cout &lt;&lt; "a2 is B";
  }
  derived = dynamic_cast&lt;B*&gt;(b);
  if (derived) {
      cout &lt;&lt; "b is B";
  }
  
  typename.cpp: In function 'int main(int, char**)':
  typename.cpp:27:36: error: cannot dynamic_cast 'a' (of type 'class A*') to type 'class B*' (source type is not polymorphic)
       B* derived = dynamic_cast&lt;B*&gt;(a);
                                      ^
  typename.cpp:31:34: error: cannot dynamic_cast 'a2' (of type 'class A*') to type 'class B*' (source type is not polymorphic)
       derived = dynamic_cast&lt;B*&gt;(a2);
  </code></pre>
  
  <p>I used static casting, but I got the answer wrong. </p>
  
  <pre><code>B* derived = static_cast&lt;B*&gt;(a);
  if (derived) {
      cout &lt;&lt; "a is B"; // -&gt; YES
  }
  derived = static_cast&lt;B*&gt;(a2);
  if (derived) {
      cout &lt;&lt; "a2 is B"; // -&gt; YES
  }
  derived = dynamic_cast&lt;B*&gt;(b);
  if (derived) {
      cout &lt;&lt; "b is B"; // -&gt; YES
  }
  </code></pre>
  
  <p>How can I correctly identify the object type in <a href="https://en.wikipedia.org/wiki/C++11" rel="nofollow">C++11</a>?</p>