void ExpandPolygon(Polygon *poly) { switch (poly->type) { case RECTANGLE: ExpandRectangle(); break; case CIRCLE: ExpandCircle(); break; // // So on and so forth... // } }There are two main issues with this code. Firstly you have the overhead of a function call just to get to the point of determining type and how to proceed. Secondly the switch statement boils down to a series of if..else's which is not fast. Given these issues then the following C++ code is typically three-four times faster.
struct Polygon { int x; int y; virtual void Expand() { x += 10; y += 10; } }; struct Rectangle : public Polygon { void Expand() { x += 10; y += 10; } }; struct Circle : public Polygon { void Expand() { x += 20; y += 20; } };Now this code is faster because the C++ compiler knows the type and resolving the call by building us a vtable (or jump table).