2013-11-26 61 views
1

我想通過基類指針從C++傳遞到Lua使用LuaBridge的對象。派生類和基類都已正確註冊到LuaBridge。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 

我怎樣才能 '鑄下' 從FooBase*Foo*在Lua? Lua/LuaBridge是否支持這個?

回答

3

可能不需要和需要這樣做可能在您的代碼中顯示設計錯誤。使用C++進行演員陣容,你知道更多關於類型的信息,而不是Lua所做的,所以Bar函數可以接受Foo *或者在調用函數之前進行downcast。