C++ 从C到Lua中的OOP
我想在我的lua解释器中实现面向对象的编程,我知道我可以从C函数返回lua表。我希望返回的表中充满C函数C++ 从C到Lua中的OOP,c++,c,oop,lua,C++,C,Oop,Lua,我想在我的lua解释器中实现面向对象的编程,我知道我可以从C函数返回lua表。我希望返回的表中充满C函数 player = getClosestPlayer(); player.walkTo(); 其中getClosestPlayer()和walkTo()都是C函数 player = getClosestPlayer(); player.walkTo(); 从walkTo()的C函数中,如何区分对象类型 我希望每个对象都有一个可以用来识别它的gid(player.gid),但是如何从c函数访
player = getClosestPlayer();
player.walkTo();
其中getClosestPlayer()和walkTo()都是C函数
player = getClosestPlayer();
player.walkTo();
从walkTo()的C函数中,如何区分对象类型
我希望每个对象都有一个可以用来识别它的gid(player.gid),但是如何从c函数访问该gid呢
换句话说,C代码中self.gid的等价物是什么
int l_playerWalkTo(lua_State* functionState){
int gid = // self.gid?
// do something with gid
}
我可以这样做的一种方法是对表中的每一个函数进行加值,但是有没有更优雅的方法呢?我也遇到过类似的问题,我解决的方法是: < P> 1)用抽象方法< /P>在C++中创建接口类
class LuaInterfaceOOP
{
public:
LuaInterfaceOOP(){}
virtual CObject* clone(void) const=0;
virtual wxString type(void)=0;
virtual wxString ToString(void)=0;
wxString GetType()return this->type();
wxString GetToString() return this->ToString();
virtual ~CObject(){}
};
2) 要向Lua公开的任何类都应该实现一致性
class MyClass: public LuaInterfaceOOP
{
public:
wxString type() { return "MyClass";}
wxString ToString();
};
3) 当您为此类编写包装器时,请确保
int MyClass_toString(lua_State* L)
{
MyClass* mc= luaW_check<MyClass>(L, 1);
const char* str=mc->ToString().c_str();
lua_pushstring(L, str);
return 1;
}
int MyClass_type(lua_State* L)
{
lua_pushstring(L,"MyClass");
return 1;
}
最后,
static int luaopen_MyClass(lua_State* L)
{
luaW_register<MyClass>(L, "MyClass", MyClass_table, MyClass_metatable, MyClass_new);
return 1;
}
static int luaopen\u MyClass(lua\u State*L)
{
luaW_寄存器(L,“MyClass”、MyClass_表、MyClass_元表、MyClass_新);
返回1;
}
现在在Lua中,您可以使用一个表达式,例如if(type(aclass)==“MyClass”)
我不确定这些步骤是否是最好的方法,但到目前为止,它起了作用。非常感谢马克洛兰的回答,我只是想澄清一下他所说的话。 这个Lua包装器可以用来实现C++类到Lua: 使用此库的一个很好的例子可以在这里找到: 下面是代码(直接取自示例站点) 卢阿: c++:
BankAccount*BankAccount\u new(lua\u州*L)
{
const char*owner=luaL_校验字符串(L,1);
浮动余额=luaL_支票号码(L,2);
返回新银行账户(所有者、余额);
}
国际银行账户存款(卢奥州*L)
{
银行账户*账户=luaW_支票(L,1);
浮动金额=luaL_支票编号(L,2);
账户->存款(金额);
返回0;
}
静态luaL_Reg BankAccount_表[]=
{
{NULL,NULL}
};
静态luaL_Reg BankAccount_元表[]=
{
{“存款”,银行账户\存款},
{NULL,NULL}
};
国际luaopen_银行账户(lua_州*L)
{
卢乌寄存器(L,
“银行账户”,
银行账户表,
BankAccount_元表,
BankAccount\u new//如果您的类具有默认构造函数,则可以忽略此参数,
//LuaWrapper将为您生成一个默认分配器。
);
返回1;
}
如您所见,使用此方法,第一个参数是对象的实例什么是luaW_检查?我似乎在lua手册中找不到它。它来自另一个库,有了这个库,我可以把整个类推到lua中吗?然后它会调用类中所有与实例相关的C++函数?这就是它的工作原理吗?使用您的结构,是否可以调用MyClass.type()?成员函数的第一个参数是对象本身的实例吗?和lua相似?对不起,如果你已经回答了我的问题,我只是还没有看到。啊,是的!我得到了它!第一个参数是对象的实例!这里可以找到一个很好的例子:。非常感谢你!这是如何将C++封装到Lua上,您可以在Web上找到许多示例。另一方面,Lua语句
if(type(ba)=“BankAccount”)
仍将不起作用我通过将类型定义为其他类型的整数作为我的“GID”推送到其他类型来实现这一点
static int luaopen_MyClass(lua_State* L)
{
luaW_register<MyClass>(L, "MyClass", MyClass_table, MyClass_metatable, MyClass_new);
return 1;
}
alicesaccount = BankAccount.new("Alice", 100)
alicesaccount:deposit(20);
alicesaccount:deposit(30);
alicesaccount:deposit(40);
BankAccount* BankAccount_new(lua_State *L)
{
const char* owner = luaL_checkstring(L, 1);
float balance = luaL_checknumber(L, 2);
return new BankAccount(owner, balance);
}
int BankAccount_deposit(lua_State *L)
{
BankAccount* account = luaW_check<BankAccount>(L, 1);
float amount = luaL_checknumber(L, 2);
account->deposit(amount);
return 0;
}
static luaL_Reg BankAccount_table[] =
{
{ NULL, NULL }
};
static luaL_Reg BankAccount_metatable[] =
{
{ "deposit", BankAccount_deposit },
{ NULL, NULL }
};
int luaopen_BankAccount(lua_State* L)
{
luaW_register<BankAccount>(L,
"BankAccount",
BankAccount_table,
BankAccount_metatable,
BankAccount_new // If your class has a default constructor you can omit this argument,
// LuaWrapper will generate a default allocator for you.
);
return 1;
}