更新时间:2023-02-15 15:14:28
您不能通过指向基类的指针来调用仅出现在派生类中的成员.首先,必须将其强制转换(可能使用 dynamic_cast
)到派生类型的指针,否则编译器甚至不知道该方法是否存在.
You can't call a member that appears only in a derived class through a pointer to the base class; you have to cast it (probably using dynamic_cast
) to a pointer to the derived type, first -- otherwise the compiler has no idea the method even exists.
它可能看起来像这样:
void someMethod(Base* bp) {
Derived *dp = dynamic_cast<Derived*>(bp);
if (dp != null)
dp->methodInDerivedClass();
}