我想做一个使用qtscript的数学编辑器。它将支持脚本中的数组计算。例如array1 + array2 =array3({1,2,3}+{3,4,5}= {4,6,8});也许我需要覆盖operator+,我参考了QByteArray的例子,我覆盖了operator+,但是当我在脚本中执行时,它不能被调用,有人能给我一些建议吗?
bytearray.h
class ByteArrayClass : public QObject, public QScriptClass
{
public:
QByteArray &operator+(int n);
}
main.cpp
int main(int argc, char **argv)
{
QCoreApplication app(argc, argv);
QScriptEngine eng;
ByteArrayClass *baClass = new ByteArrayClass(&eng);
eng.globalObject().setProperty("ByteArray", baClass->constructor());
eng.evaluate("ba = new ByteArray(4))"
eng.evaluate("ba+2;"); //this will not invoke override operator+.
ByteArrayClass *ba = new ByteArrayClass(&eng);
int n = 3;
*ba + n; //but this can invoke the override operator+
}如果这不能实现,可能的一种方法是将所有运算符替换为自定义函数。
发布于 2010-08-05 02:47:58
据我所知,运算符不能在QtScript中重载,因为它在Javascript中通常是不允许的(例如,参见ECMA Script 4 - Progress和本Article)。
现在,对于您的情况,您可以选择Add、Mult、...函数,或者留给一些受约束较少的脚本语言。
https://stackoverflow.com/questions/3288651
复制相似问题