c++ - 从“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);
}
但到目前为止没有运气!
解决方案
常规指针与指向成员的指针不同。由于reinterpret_cast
转换(请参阅https://en.cppreference.com/w/cpp/language/reinterpret_cast),指针可以转换为另一个指针或整数类型,但不能转换为指向成员的指针
推荐阅读
- mysql - 如何获取客户首次订单的数量?
- spring - 如何以与 Spring bean 定义相同的顺序获取集合元素
- hadoop - 数据中的不良记录以及将它们写入单独的文件后如何将它们加载到配置单元中
- node.js - 如何在 Synology NAS 上设置 React 项目
- android - 如何使用 Kotlin 在 Android 中保存开关设置
- python - 如何将用破折号“-”分隔的数字与另一个数字相乘?
- java - 迭代时调用数组的长度属性两次或创建一个新变量更快吗?
- vue.js - vuejs如何通过点击跨度来增加每个游戏的收视率?
- javascript - 为什么 vue.js 组件在第一次尝试时不显示获取的数据?
- google-apps-script - 基于单元格编辑在工作表之间移动行 + 通过列查找找到的空白行不是整行