0

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?

Tomáš Zato
  • 50,171
  • 52
  • 268
  • 778
  • Have you tried doing what the original error message suggested? – nwp Sep 14 '17 at 14:51
  • @nwp That's what you can see in the last example – Tomáš Zato Sep 14 '17 at 14:52
  • The last example shows an insane C cast of pointers to member function. That's not at all what the error message suggested. – nwp Sep 14 '17 at 14:53
  • Also the function you are showing has a different signature than [the expected function signature](https://doc.qt.io/qt-5/qscriptengine.html#FunctionSignature-typedef). Casting a function pointer will not somehow provide the `ScriptProcessContext` required to call the function you want it to call. – nwp Sep 14 '17 at 14:57
  • @nwp It's not insane at all and has no effect, the error is the same when one comments it out. Which is unsurprising since I just literally wrote cast to cast it to the function I defined earlier. – Tomáš Zato Sep 14 '17 at 14:57

1 Answers1

1

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.

king_nak
  • 11,313
  • 33
  • 58