从 Lua 初始化的 const double
我有一个全局变量:
const double myvar = 5.1;
现在,我将其转换为从 Lua 读取这些值。
但是,我不能简单地这样做:
const double myvar = lua_tonumber(L,1);
因为 main() 必须首先执行才能启动 Lua 解释器等,但如果我之后声明 myvar,它将不是全局的。
有什么方法可以实现从 Lua 获取其值的全局 const 变量吗?
I have a global variable:
const double myvar = 5.1;
Now, I'm converting this to read these values from Lua.
However, I can't simply do:
const double myvar = lua_tonumber(L,1);
Since main() must first execute to start the Lua intepreter etc., but if I declare myvar afterwards, it will not be global.
Is there any way to do achieve a global const variable which takes it's value from Lua?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
const
的微妙后果只有语言律师才能完全理解,但 const 变量 的基本思想是它的值在编译时指定。 只有存在 Lua 解释器才能创建 Lua 值,这需要调用lua_open()
,而这只有在运行时才能完成。 所以不,没有(安全、理智)的方式让一个const
变量的值由Lua决定。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 callinglua_open()
, which cannot be done until run time. So no, there is no (safe, sane) way of having aconst
variable whose value is determined by Lua.您可以像这样违反常量:
但这是一种非常糟糕的做法。
编辑:您可以这样做,而不是声明 const 变量:
甚至可以这样做:
You can violate constness like this:
but it's a -very- bad practice.
Edit: Instead of declaring const variables you can do this:
or even this: