title: C++ - Is it possible to check derived class type in multiple inheritance?
body: <p>In the code that is below I want to count the number of occurrences of objects of <code>class B</code> from the <code>std::vector</code> using <code>dynamic_cast</code> conversion. But the <strong>result is 2</strong> and it should be <strong>1</strong>. This happens because <code>dynamic_cast</code> checks object of <code>class D</code> as an object of <code>class B</code> because of the inheritance.</p>
  
  <p>Is there any possible way to check the derived class type for multiple inheritance without having this problem? Is there any Java instanceof equivalent in C++ for this situation?</p>
  
  <pre><code>// Example program
  #include &lt;iostream&gt;
  #include &lt;string&gt;
  #include &lt;vector&gt;
  
  
  using namespace std;
  
  class Base{
  
  public:  
  Base(){};
  virtual ~Base(){};
  };
  
  class A:public virtual Base{
  public:
  A() {};
  virtual ~A(){};
  
  };
  
  class B:public virtual Base{
  public:
  B(){};
  virtual ~B(){};
  
  
  };
  
  
  class D: public A, public B{
  public:
  
  D(){};
  virtual ~D(){};
  
  };
  
  int main()
  {
  
   int c=0;
  std::vector&lt;Base*&gt; v;
  std::vector&lt;Base*&gt;::iterator myIt;
  
  v.push_back(new Base());
  v.push_back(new A());
  v.push_back(new B());
  v.push_back(new D());
  
  for(myIt=v.begin(); myIt!=v.end();myIt++)
      if(B* object=dynamic_cast&lt;B*&gt;(*myIt))
          c++;
  cout&lt;&lt;c&lt;&lt;endl;
  return 0;
  }
  </code></pre>