【问题标题】:How do I prevent the creation of a new property in a Luabind class?如何防止在 Luabind 类中创建新属性?
【发布时间】:2011-09-06 11:15:45
【问题描述】:

我正在使用Luabind 将 C++ 类绑定到 Lua。从 Lua 脚本中,我可以将任意属性(键/值对)添加到代表我的类的表中,即使我从未告诉 Luabind 这些属性存在:

cheese = Cheese()
cheese.type = "gouda"
cheese.age = 2
cheese.hippopotamus = "monkey" -- Oops! This member was not defined!

河马线虽然荒谬,但执行时不会导致 Lua 错误。

如何防止这些新属性?我知道当只使用 Lua C API 时,这可以使用 __newindex 元方法来完成,但是有没有办法用 Luabind 做到这一点?

【问题讨论】:

    标签: c++ lua luabind


    【解决方案1】:

    我没有经验,但这里有一些来自 luabind-user 邮件列表的电子邮件线程:首先,a monologue by a guy trying to do exactly what you want。其次,来自 luabind 维护者的有点旧的(2009 年 11 月)电子邮件,指出 providing custom __newindex metamethod is not currently possible, but planned for 1.0(当前版本是 0.9.something)。

    【讨论】:

      猜你喜欢
      • 2020-04-10
      • 2011-04-05
      • 2014-09-26
      • 2013-09-08
      • 1970-01-01
      • 1970-01-01
      • 2011-05-02
      • 2021-03-15
      • 1970-01-01
      相关资源
      最近更新 更多