title: C++ Downcasting to Derived Class based off Variable
body: <p>Suppose I have a base class "Shape", and derived classes "Triangle", "Square", and "Circle".
  A member of "Shape" is an int "shapeType". </p>
  
  <p>If shapeType==1, then it's a triangle.
  If shapeType==2, then it's a square.
  If shapeType==3, then it's a circle.</p>
  
  <p>I'm interested in knowing given I have just a "Shape" object that was once a derived-object, if there is a way to "dynamically" down-cast to the proper derived class by using the shapeType value.</p>
  
  <p>I know I can do a hard-code switch statement, roughly like:</p>
  
  <pre><code>Triangle* t;
  Square* s;
  Circle* c;
  
  switch (shape-&gt;shapeType) {
  case 1:
     t = (Triangle*)shape;
  case 2: 
     ...
  }
  </code></pre>
  
  <p>However, the above requires me to make a pointer of EVERY derived class possibility. I am wondering if there is a way to do this without hard-coding every class, but instead somehow determine the class type map where the key is the shapeType and the value is the class type.</p>