首页 > 解决方案 > 从“void*”类型到类成员函数指针的无效转换

问题描述

typedef A::T*(Processor::*myMethodType)(const FDS::T*);

myMethodType temp = NULL;

temp = reinterpret_cast<myMethodType> (myInterface.findMap(moname)))

注意:处理器是一个实例化模板类的类。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);
}

但到目前为止没有运气!

标签: c++reinterpret-cast

解决方案


常规指针与指向成员的指针不同。由于reinterpret_cast转换(请参阅https://en.cppreference.com/w/cpp/language/reinterpret_cast),指针可以转换为另一个指针或整数类型,但不能转换为指向成员的指针


推荐阅读