0

context: I have an code where I have functions based on MsgID's, all these functions corresponding to MSGID's will be kept in MAP which is in different class. MSGID's would be received in different class and I am trying to pass to MAP which is in other class for getting the function pointer and call the function with parameters.

Below is the fucntion in a class where i receive data and use msg id.
    virtual BOOL _nanoMsgCallBack( VOIDPTR lpv )
    {
        LONG id;
        cout << " In virtual BOOL OnTask( LPVOID lpv ) in main \n";
        nanoMsgThreadId(&id);
        if( lpv )
        {
            //int *pInt = (int *)lpv;
            args *data  = (struct args*)lpv;
            interface = test->getInstanceForMsgID(data->MsgID);
            FUNCPTR func = test->getFunctionPrt(data->MsgID);
            //(interface->*func)(lpv);
            (interface->*func)(lpv); 

            //dont' use cout here, output could be broken up due to threading
            printf("\tthread(%ld, MsgID=%d, and DevID=%d \n",id,data->MsgID,data->DevID);
        }
        return TRUE;
    }
This is how i am declaring my MAP

typedef void (*FUNCPTR)(VOIDPTR);
class clMsgIDMapping
{

private:

    IBackendInterface *testClass;
    std::map<int, FUNCPTR> _mapingTable;

    void setCallback(int MSGID, FUNCPTR funcptr);//This will be called by other classes for setting the Fucntions.

    IBackendInterface* getInstanceForMsgID(int MsgID);

    FUNCPTR getFunctionPrt(int MsgID);
}

Implementation of getFunctionPrt(int MssgID) and getInstanceForMsgID is as follows

IBackendInterface* clMsgIDMapping::getInstanceForMsgID(int MsgID){

    switch(clMsgIDMapping::_sMap.at(MsgID))
    {

    case enmMsgType_Request :
        //return DBUS adapter instance
        return testClass;
        break;
    case enmMsgType_Response:
        //return nanoMsg sender instance
        break;
    case enmMsgType_Event:
        //return DBUS adapter instance
        break;
    case enmMsgType_Blocked:
        //TBD
        break;
    default:
        break;
    }
    return NULL;
}

FUNCPTR clMsgIDMapping::getFunctionPrt(int MsgID){
    return _mapingTable.at(MsgID);
}

This is where setCallback would happen.

#include "Test.h"

Test::Test() {
    // TODO Auto-generated constructor stub
    _instance = clMsgIDMapping::getInstance();
    _instance->setCallback(0x0113, (FUNCPTR)&Test::testFunc);
}

Test::~Test() {
    // TODO Auto-generated destructor stub
}
void Test::Register(){
    // called
}

void Test::testFunc(void* testf){
    std::cout<<"test func called\n";
}


(interface->*func)(lpv); // this line is my error area
Stu
  • 13
  • 3
  • You seem to be confusing function pointers and method pointers -- they are two different incompatible things and you can't cast one to the other. – Chris Dodd Feb 06 '20 at 03:57

1 Answers1

0

define your function pointer as member function pointer like

typedef void (IBackendInterface::*FUNCPTR)(VOIDPTR);
idris
  • 488
  • 3
  • 6