How do I cast class method to QScriptEngine::FunctionSignature

101 views Asked by At

I want to pass this method:

QScriptValue ScriptProcessContext::construct(QScriptContext * ctx, QScriptEngine *)
{
    return this->newInstance();
}

to QScriptEngine::newFunction. I tried the following options:

  • As in documentation and also this example code:

    QScriptValue ctor = engine->newFunction(construct);
    

    Error:

    error C3867: 'ScriptProcessContext::construct': function call missing argument list; use '&ScriptProcessContext::construct' to create a pointer to member
    
  • Force the cast:

    QScriptValue ctor = engine->newFunction((QScriptEngine::FunctionSignature)construct);
    

    Error:

    error C2440: 'type cast' : cannot convert from 'overloaded-function' to 'QScriptEngine::FunctionSignature'
    
  • Cast the pointer to member

    QScriptValue ctor = engine->newFunction(
       (QScriptValue(ScriptProcessContext::*)(QScriptContext *, QScriptEngine *))
        &ScriptProcessContext::construct
    );
    

    Error:

     error C2664: 'QScriptValue QScriptEngine::newFunction(QScriptEngine::FunctionSignature,int)' : cannot convert parameter 1 from 'QScriptValue (__cdecl ScriptProcessContext::* )(QScriptContext *,QScriptEngine *)' to 'QScriptEngine::FunctionSignature'
    

So how to write it correctly?

1

There are 1 answers

1
king_nak On BEST ANSWER

You try to pass a memberfunction. That is not supported. Use a free function or a static method.

Note that in both cases, you won't have a this pointer. You need to write the function in a way that it doesn't need an object.