Академический Документы
Профессиональный Документы
Культура Документы
(RTTI)
• At such times you should use RTTI. You can derive a new
class from the base class and add your extra member
function to it.
• int r, c, t;
• for (i = 0; i < 10, i + +) // generate and count objects
• {ptr = factory( ); // generate an object
• cout << “Object is “ << typeid(*ptr).name( ) << endl;
• if (typeid(*ptr) == typeid(Rectangle) r++;
• if (typeid(*ptr) == typeid(Circle) c++;
• if (typeid(*ptr) == typeid(Triangle) t++; }
• cout << “figures generated:\n”;
• cout << “rectangles: “ << r << endl;
• cout << “circles: “ << c << endl;
• cout << “triangles: “ << t << endl;
• return 0;
• }
bp=&bobj;
dp= dynamic_cast<Derived *> (bp);
if (!dp) cout<<“Cast fails”;