答复: how to derive lua class fr om c++ class

"kunshou" <[email protected]> Thu, 12 Jan 2012 22:42:02 +0800
Newsgroups gmane.comp.lang.lua.bind.user
Message-ID <[email protected]>
Thanks so much. 

I misunderstood the manual the first time I read it

Anyway, Problem solved!

 

 

发件人: Алексей [mailto:[email protected]] 
发送时间: 2012年1月9日 1:57
收件人: [email protected]
主题: Re: [luabind] how to derive lua class from c++ class

 

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

------------------------------------------------------------------------------
RSA(R) Conference 2012
Mar 27 - Feb 2
Save $400 by Jan. 27
Register now!
http://p.sf.net/sfu/rsa-sfdev2dev2

_______________________________________________
luabind-user mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/luabind-user