从Lua初始化的const双 [英] Const double initialised from Lua

查看:144
本文介绍了从Lua初始化的const双的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个全局变量:

const double myvar = 5.1;

现在,我正在将其转换为从Lua读取这些值.

Now, I'm converting this to read these values from Lua.

但是,我不能简单地这样做:

However, I can't simply do:

const double myvar = lua_tonumber(L,1);

由于main()必须首先执行才能启动Lua解释器等,但是如果我之后声明myvar,它将不是全局的.

Since main() must first execute to start the Lua intepreter etc., but if I declare myvar afterwards, it will not be global.

有什么方法可以实现从Lua获取其值的全局const变量吗?

Is there any way to do achieve a global const variable which takes it's value from Lua?

推荐答案

只有语言律师才能完全理解const的细微差别,但是const 变量的基本思想是它的值在编译时指定.在没有Lua解释器之前,不能创建Lua值,该解释器要求调用lua_open(),直到运行时才能完成.因此,,没有(安全,理智的)方式拥有由Lua决定其值的const变量.

The subtle ramifications of const can be fully understood only by language lawyers, but the basic idea of a const variable is that its value is specified at compile time. Lua values cannot be created until there is a Lua interpreter, which requires calling lua_open(), which cannot be done until run time. So no, there is no (safe, sane) way of having a const variable whose value is determined by Lua.

这篇关于从Lua初始化的const双的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆