如何将 class 方法转换为 QScriptEngine::FunctionSignature
How do I cast class method to QScriptEngine::FunctionSignature
我想通过这个方法:
QScriptValue ScriptProcessContext::construct(QScriptContext * ctx, QScriptEngine *)
{
return this->newInstance();
}
到QScriptEngine::newFunction
。我尝试了以下选项:
如文档中所述and also this example code:
QScriptValue ctor = engine->newFunction(construct);
错误:
error C3867: 'ScriptProcessContext::construct': function call missing argument list; use '&ScriptProcessContext::construct' to create a pointer to member
强制转换:
QScriptValue ctor = engine->newFunction((QScriptEngine::FunctionSignature)construct);
错误:
error C2440: 'type cast' : cannot convert from 'overloaded-function' to 'QScriptEngine::FunctionSignature'
将指针转换为成员
QScriptValue ctor = engine->newFunction(
(QScriptValue(ScriptProcessContext::*)(QScriptContext *, QScriptEngine *))
&ScriptProcessContext::construct
);
错误:
error C2664: 'QScriptValue QScriptEngine::newFunction(QScriptEngine::FunctionSignature,int)' : cannot convert parameter 1 from 'QScriptValue (__cdecl ScriptProcessContext::* )(QScriptContext *,QScriptEngine *)' to 'QScriptEngine::FunctionSignature'
那么怎么写才是正确的呢?
您尝试传递一个成员函数。那是不支持的。使用自由函数或静态方法。
请注意,在这两种情况下,您都不会有 this
指针。您需要以不需要对象的方式编写函数。
我想通过这个方法:
QScriptValue ScriptProcessContext::construct(QScriptContext * ctx, QScriptEngine *)
{
return this->newInstance();
}
到QScriptEngine::newFunction
。我尝试了以下选项:
如文档中所述and also this example code:
QScriptValue ctor = engine->newFunction(construct);
错误:
error C3867: 'ScriptProcessContext::construct': function call missing argument list; use '&ScriptProcessContext::construct' to create a pointer to member
强制转换:
QScriptValue ctor = engine->newFunction((QScriptEngine::FunctionSignature)construct);
错误:
error C2440: 'type cast' : cannot convert from 'overloaded-function' to 'QScriptEngine::FunctionSignature'
将指针转换为成员
QScriptValue ctor = engine->newFunction( (QScriptValue(ScriptProcessContext::*)(QScriptContext *, QScriptEngine *)) &ScriptProcessContext::construct );
错误:
error C2664: 'QScriptValue QScriptEngine::newFunction(QScriptEngine::FunctionSignature,int)' : cannot convert parameter 1 from 'QScriptValue (__cdecl ScriptProcessContext::* )(QScriptContext *,QScriptEngine *)' to 'QScriptEngine::FunctionSignature'
那么怎么写才是正确的呢?
您尝试传递一个成员函数。那是不支持的。使用自由函数或静态方法。
请注意,在这两种情况下,您都不会有 this
指针。您需要以不需要对象的方式编写函数。