1

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*に「キャストダウン」するにはどうすればよいですか? Foo*Lua/LuaBridge はこれをサポートしていますか?

4

1 に答える 1

3

おそらくそうではなく、そうする必要があると、コードの設計ミスが示される可能性があります。C++ でキャストを実行すると、Lua よりも型についてはるかによく知っているため、関数 Bar が Foo* を受け入れるか、関数を呼び出す前にダウンキャストを実行します。

于 2013-11-26T22:22:45.647 に答える