說我,我有這些類:有沒有辦法使用dynamic_cast投射到孩子?
struct Parent {};
struct Child : public Parent {
void func() {}
};
現在說,我想創造這樣的功能:
void foo(Parent* arg) {
auto child = dynamic_cast<Child*>(arg);
if(child != nullptr) child->func();
}
但很明顯,這將明顯地給我的錯誤:
dynamic_cast
:Parent
is not a polymorphic type
所以我不能做的dynamic_cast
步驟,有沒有一種方法,我可以驗證arg
其實是一個Child*
在運行時?