相关疑难解决方法(0)

以编程方式进行C++编译:可以完成吗?

假设我有一个Base类和几个Derived类.有没有办法将对象强制转换为派生类之一,而无需编写如下内容:


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 {
  ...
}
Run Code Online (Sandbox Code Playgroud)

c++ casting

5
推荐指数
3
解决办法
1051
查看次数

标签 统计

c++ ×1

casting ×1