从类型"void*"到类member_function指针的强制转换无效
invalid cast from type âvoid*â to class member_function pointer
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( 指针可以转换为另一个指针或整型类型,但不能转换为指向成员的指针
不能在函数指针和对象指针(如void*
(之间进行强制转换。如果需要具有多个函数指针类型的映射,可以将它们转换为更多的函数指针类型,例如void*(*)()
。函数指针在调用之前必须转换回原始类型。
如果不需要多个函数指针类型,则可以使用myMethodType
映射。
相关文章:
- 正在将指针转换为范围
- 如何理解将半精度指针转换为无符号长指针和相关的内存对齐
- C++:Lambda 函数指针转换的用例是什么?
- 如何将 int 指针转换为浮点指针
- 为什么在将 void 指针转换为整数指针时出现分段错误
- 将(N 个字节)无符号字符指针转换为浮点数和双 C++
- 我们可以在不知道其真实类型的情况下将基类指针转换为派生类指针吗?
- C++中数组大小未知的指针转换
- Antlr cpp 运行时 任何错误的指针转换?
- std::flush可以用于将对象指针转换为其封闭数组指针吗
- C 指针转换会导致内存访问冲突
- 模板类实例化中的指针转换无效
- 函数调用歧义(用户定义的转换和 Derived2Base 指针转换)
- 如何将 void(*)() 类型的指针转换为 void*
- 数组到指针转换期间的临时具体化
- 直接为浮点变量分配十六进制整数与通过指针转换分配之间的区别
- 将基类指针转换为派生的类指针
- 应对 std::字符串中的 std::<char>指针转换后的向量
- 在编译时将函数指针转换为 std::uintptr_t
- 将基类指针转换为未知派生类指针