2013-11-26 76 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。