基本クラスといくつかの派生クラスがあるとしましょう。次のようなものを書く必要なく、派生クラスの 1 つにオブジェクトをキャストする方法はありますか?
string typename = typeid(*object).name();
if(typename == "Derived1") {
Derived1 *d1 = static_cast< Derived1*>(object);
}
else if(typename == "Derived2") {
Derived2 *d2 = static_cast < Derived2*>(object);
}
...
else {
...
}