【问题标题】:LuaBridge and InheritanceLuaBridge 和继承
【发布时间】:2013-12-12 05:20:27
【问题描述】:

我正在尝试使用 LuaBridge 通过基类指针将对象从 C++ 传递到 Lua。派生类和基类都已正确注册到 LuaBridge。

在 C++ 方面:

// Assume both Foo and FooBase are registered properly with LuaBridge,
// exposing the a, b, and c properties

struct FooBase
{
    int a;
    // ...
};

struct Foo : public FooBase
{
    int b;
    int c;
    // ...
};

// ... other code ...

void Bar(const FooBase* foo)
{
    // Assume that 'foo' is a pointer to a valid 'Foo' object
    luabridge::LuaRef ref = luabridge::getGlobal(L, "bar");
    ref(foo);
}    

在 Lua 方面:

function bar(foo)
    foo.a -- This is ok
    foo.b -- This is nil
end

如何在 Lua 中将 FooBase* 'cast-down' 到 Foo*? Lua/LuaBridge 甚至支持这个吗?

【问题讨论】:

    标签: c++ inheritance lua luabridge


    【解决方案1】:

    可能不会,如果需要这样做可能在您的代码中显示设计错误。在 C++ 中进行强制转换,您对类型的了解远多于 Lua,因此函数 Bar 接受 Foo* 或在调用函数之前进行向下转换。

    【讨论】:

      猜你喜欢
      • 2014-04-22
      • 1970-01-01
      • 1970-01-01
      • 2020-10-07
      • 2015-12-09
      • 1970-01-01
      • 1970-01-01
      • 2020-06-19
      • 2011-05-28
      相关资源
      最近更新 更多