0

I'm trying to call a pointer to a member function, which I retrieve from a map. The call th.fpObjHandler(md, evd, tokenIdx) seems erroneous, I've tried various different syntaxes (e.g. .* and ->*), but I cannot seem to get it right. Hopefully somebody here is able to help.

struct TokenHandler
{
    int tokenIdx;
    eventDataStatus_t (Schema::*fpObjHandler)(MessageData &md, EventDataImpl &evd, int &tokenIdx);
};

Schema::event(MessageData &md, EventDataImpl &evd)
{
    int tokenIdx = 0;
    std::map<std::string, TokenHandler> tokenHandlerMap;
    tokenHandlerMap["timeInterval"] = { -1, &Schema::timeInterval };

    // ..
    // ....

    TokenHandler th = tokenHandlerMap.at(key);
    if (th.fpObjHandler != NULL) {
        th.fpObjHandler(md, evd, tokenIdx); // THIS RESULTS IN ERROR
        //..
        //...
    }
}

eventDataStatus_t Schema::timeInterval(MessageData &md, EventDataImpl &evd, int &tokenIdx)
{
    //..
    //...
    return OK;
}

Schema.cpp:111:54: error: must use ‘.’ or ‘->’ to call pointer-to-member function in ‘th.TokenHandler::fpObjHandler (...)’, e.g. ‘(... ->* th.TokenHandler::fpObjHandler) (...)’ th.fpObjHandler(md, evd, tokenIdx); ^

Gio
  • 3,242
  • 1
  • 25
  • 53
  • @ZeroUltimax, if I would write `(th.*fpObjHandler)(md, evd, tokenIdx)`, than this would result in the error `fpObjHandler was not declared in this scope`, therefore I consider this problem different and not a duplicate. – Gio Mar 03 '17 at 16:38

1 Answers1

2

First you need an instance of the class Schema, then use .* or ->*, something like:

Schema schema;

(schema.*th.fpObjHandler)(md, evd, tokenIdx);

Or, as you are already in a method of Schema:

(this->*th.fpObjHandler)(md, evd, tokenIdx);
Jarod42
  • 203,559
  • 14
  • 181
  • 302