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

myMethodType temp = NULL;

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

Note: Processor is a class which instantiated Template class . myMethodType is the member-pointer-func pointer .

Note : findMap is returning a void* as seen below and myinterface is a map of

template <class Implementor>

void* Interface<Implementor>::findMap(std::string &name){
    if (myInterface.find(moname) != myInterface.end()) {
        return myInterface.find(moname)->second;
    }

    return NULL;
}

========================================================

Getting below error -

error: invalid cast from type âvoid*â to type âProcessor::myMethodType {aka T* (Processor::*)(const T*)}â
      if((temp = reinterpret_cast<myMethodType> (myInterface.findMap(moname))) != NULL)

Question - why am I getting this error, though I am casting it to member_class_pointer? why Invalid conversion?

Tried till now :

Tried below approach from --- 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);
}

but no luck so far !!

dontask
  • 11
  • 2

2 Answers2

0

You cannot cast between function pointers and object pointers (such as void*). If you want a map with multiple function pointer types, you can convert them to some more function pointer type, such as void*(*)(). Function pointers must be converted back to the original type before calling them.

If you don't need multiple function pointer types, you can use a map of myMethodType.

VLL
  • 9,634
  • 1
  • 29
  • 54
0

A regular pointer is different from pointer-to-member. Since reinterpret_cast conversion (see https://en.cppreference.com/w/cpp/language/reinterpret_cast) a pointer can be converted to another pointer or to an integral type, but not to pointer-to-member

P. Dmitry
  • 1,123
  • 8
  • 26
  • but void pointer does not know which type it is pointing to ? so it can not be considered as a regular pointer, it may be a pointer to member, please correct me. – dontask Sep 11 '19 at 08:11
  • Pointer and pointer-on-member it's just different types. You can not cast it to each other. See https://en.cppreference.com/w/cpp/language/pointer `Typically, mentions of "pointers" without elaboration do not include pointers to (non-static) members.` – P. Dmitry Sep 11 '19 at 08:16
  • Probably you should change return type on `void*` for `myMethodType` and return `myMethodType` from `find` – P. Dmitry Sep 11 '19 at 08:21
  • We cant change to myMethodType , as this is a template class – dontask Sep 12 '19 at 08:30
  • Do you implement this class? If so, you can freely change the return type of methods to `void*` – P. Dmitry Sep 12 '19 at 08:38