Re: how to derive lua class from c++ class
Алексей <[email protected]> Sun, 8 Jan 2012 19:56:55 +0200
| Newsgroups | gmane.comp.lang.lua.bind.user |
|---|---|
| Message-ID | <CAKWY9ZRBO19SOaAMWg4Zdzfa4wsxB_szAV54Zt8iP7zPsm0BXg@mail.gmail.com> |
Hi
Here <http://www.rasterbar.com/products/luabind/docs.html#deriving-in-lua>is
very clear described how to derive a class in Lua from C++ class.
For your example you should have something like that:
class testclass
{
public:
testclass(const char* s): m_string(s)
{std::cout << m_string << " @created." << std::endl;}
*virtual* void print_string() { std::cout <<"testclass:" <<m_string <<
"\n"; }
private:
std::string m_string;
};
struct testclass_wrapper : testclass, luabind:wrap_base
{
testclass_wrapper(const char* s):
testclass(s) {}
virtual void print_string()
{
luabind::call_member<void>(this,"print_string");
}
static void default_print_string(testclass * ptr)
{
(ptr->testclass::print_string());
}
};
...
module(L)
[
class_<testclass, testclass_wrapper>("testclass")
.def(constructor<const char*>())
.def("print_string", &testclass::print_string, &
testclass_wrapper::default_print_string)
];
The Lua code:
class 'derivedclass' (testclass)
function derivedclass:__init(name)
testclass.__init(self, name)
end
function derivedclass:print_string()
testclass.print_string(self)
print("This is derived class in Lua")
end
testderivedclass = derivedclass("hi")
testderivedclass:print_string()
2012/1/8 kunshou <[email protected]>
> ** **
>
> ** **
>
> *发件人:* kunshou [mailto:[email protected]]
> *发送时间:* 2012年1月2日 2:24
> *收件人:* [email protected]
> *主题:* how to derive lua class from c++ class****
>
> ** **
>
> Hi****
>
> I just stepped into luabind 0.9.1. I am trying to derive a class in lua
> from a c++ class.****
>
> My c++ class looks like****
>
> class testclass****
>
> {****
>
> public:****
>
> testclass(const char* s): m_string(s) ****
>
> {std::cout << m_string << " @created." << std::endl;}****
>
> void print_string() { std::cout <<"testclass:" <<m_string << "\n"; }**
> **
>
> ** **
>
> private:****
>
> std::string m_string;****
>
> };****
>
> ** **
>
> The lua code****
>
> ** **
>
> class 'derivedclass' (testclass)****
>
> function derivedclass:__init(name)****
>
> testclass:__init(name);****
>
> end****
>
> ** **
>
> testderivedclass = derivedclass("hi")****
>
> testderivedclass:print_string()****
>
> ** **
>
> when I run this lua code in c++ using luaL_dofile, the program always
> crashes.****
>
> ** **
>
> Could anyone please tell what’s wrong with my code, or give a right
> example?****
>
> ** **
>
> Many thanks****
>
>
> ------------------------------------------------------------------------------
> Ridiculously easy VDI. With Citrix VDI-in-a-Box, you don't need a complex
> infrastructure or vast IT resources to deliver seamless, secure access to
> virtual desktops. With this all-in-one solution, easily deploy virtual
> desktops for less than the cost of PCs and save 60% on VDI infrastructure
> costs. Try it free! http://p.sf.net/sfu/Citrix-VDIinabox
> _______________________________________________
> luabind-user mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/luabind-user
>
>
--
Best regards,
Aliaksei Baiburyn
------------------------------------------------------------------------------
Ridiculously easy VDI. With Citrix VDI-in-a-Box, you don't need a complex
infrastructure or vast IT resources to deliver seamless, secure access to
virtual desktops. With this all-in-one solution, easily deploy virtual
desktops for less than the cost of PCs and save 60% on VDI infrastructure
costs. Try it free! http://p.sf.net/sfu/Citrix-VDIinabox
_______________________________________________
luabind-user mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/luabind-user