Suppose I have a base class “Shape”, and derived classes “Triangle”, “Square”, and “Circle”.
A member of “Shape” is an int “shapeType”.
If shapeType==1, then it’s a triangle.
If shapeType==2, then it’s a square.
If shapeType==3, then it’s a circle.
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.
I know I can do a hard-code switch statement, roughly like:
Triangle* t;
Square* s;
Circle* c;
switch (shape->shapeType) {
case 1:
t = (Triangle*)shape;
case 2:
...
}
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.
If they’ve virtual functions, then use
dynamic_cast:But take a note: you should try defining the classes and virtual functions in such a way that you would hardly need to use
dynamic_cast. Prefer well-defined interface, and polymorphism, in general.Here is one example,
No need to use
shapeTypevariable.