我在我的代码中使用Tcl_CreateObjCommand(interp, cmdName, proc, clientData, deleteProc)并将 DerivedClass 指针传递给 clientData 参数。在回调函数中,我想将 clientData 键入安全转换(dynamic_cast)回 DerivedClass 指针,但 gcc 编译器抱怨“ source is not a class to pointer
”。这是因为 clientData 是一种 void 指针。在这个用例中,开发人员在使用 Tcl API 时通常会如何处理这个问题?
int main()
{
...
Tcl_CreateObjCommand(interp, cmdName, myCallback, myDerivedClassPointer, (Tcl_CmdDeleteProc *)NULL);
}
myCallback(ClientData clientData, Tcl_Interp *interp, int objc, Tcl_Obj* const objv[])
{
// I want to do something like this, so when the pointer is not a DerivedClassPointer type, throw an exception.
DerivedClass* foo = dynamic_cast<DerivedClass*>(clientData);
if(!foo) throw exception, type conversion fail
}