我正在使用Luabind将 C++ 类绑定到 Lua。从 Lua 脚本中,我可以将任意属性(键/值对)添加到代表我的类的表中,即使我从未告诉 Luabind 这些属性存在:
cheese = Cheese()
cheese.type = "gouda"
cheese.age = 2
cheese.hippopotamus = "monkey" -- Oops! This member was not defined!
hippopotamus 行虽然很荒谬,但执行时不会导致 Lua 错误。
如何防止这些新属性?我知道当只使用 Lua C API 时,这可以使用 __newindex 元方法来完成,但是有没有办法用 Luabind 做到这一点?