typedef A::T*(Processor::*myMethodType)(const FDS::T*);
myMethodType temp = NULL;
temp = reinterpret_cast<myMethodType> (myInterface.findMap(moname)))
注意: Processor是实例化Template类的类。 myMethodType是成员指针功能指针。
注意:findMap返回一个void *,如下所示,而myinterface是
的映射template <class Implementor>
void* Interface<Implementor>::findMap(std::string &name){
if (myInterface.find(moname) != myInterface.end()) {
return myInterface.find(moname)->second;
}
return NULL;
}
================================================ =========
遇到错误-
error: invalid cast from type âvoid*â to type âProcessor::myMethodType {aka T* (Processor::*)(const T*)}â
if((temp = reinterpret_cast<myMethodType> (myInterface.findMap(moname))) != NULL)
问题-为什么我将其强制转换为member_class_pointer却出现此错误? 为什么转换无效?
尝试到现在为止:
从---尝试了以下方法
https://stackoverflow.com/questions/1307278/casting-between-void-and-a-pointer-to-member-function
void myclass::static_fun(myclass *instance, int arg)
{
instance->nonstatic_fun(arg);
}
但到目前为止还没有运气!
答案 0 :(得分:0)
您不能在函数指针和对象指针(例如void*
)之间进行强制转换。如果要使用多种函数指针类型的映射,可以将它们转换为更多的函数指针类型,例如void*(*)()
。在调用函数指针之前,必须将其转换回原始类型。
如果不需要多种函数指针类型,则可以使用myMethodType
的映射。
答案 1 :(得分:0)
常规指针不同于成员指针。自reinterpret_cast
转换以来(请参见https://en.cppreference.com/w/cpp/language/reinterpret_cast)
指针可以转换为其他指针或整数类型,但不能转换为成员指针