【发布时间】: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 做到这一点?
【问题讨论】: