title: How do I get the derived class from a interface?
body: <p>While trying to create an Entity-Component-System in C++, I have faced some problems regarding by lack of knowledge on the language.</p>
  
  <p>With a class <strong>Entity</strong>, that holds the interface <strong>IComponent</strong> (which acts more like a flag that says "I hold data"), I have a method <strong>Add</strong> that adds a Component to the Entity if there is not another IComponent of the same class already in it.</p>
  
  <p>Here's an oversimplified sample code:</p>
  
  <pre><code>struct IComponent{};
  
  struct Foo : IComponent{ int x;};
  struct Bar : IComponent{ int y; };
  
  class Entity{
      vector&lt;IComponent*&gt; entityComponents;
  
      void Add(IComponent* componentToAdd){
          if("entityComponents" does not contain the class of "componentToAdd")
              entityComponents.add (componentToAdd)
  
      }
  }
  </code></pre>
  
  <p>My expected result would be</p>
  
  <pre><code>Entity e;
  Foo f;
  Bar b;
  Foo anotherF;
  
  e.Add(f); // Works
  e.Add(b); // Works
  e.Add(anotherF); // Does not work because another 
                   //item in the array already inherits Foo
  </code></pre>
  
  <p>But I do not know how to get the base class of Foo and Bar from inside the IComponents list and check if they are repeated.</p>
  
  <p>How may I get them? How may I cast an IComponent to a Foo if Foo is in the IComponent list?</p>