Derive C++ Class in Lua

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Derive C++ Class in Lua

Emmanuel Barroga
I am trying to derive my base class in Lua. My code in Lua looks like this:

class 'square' (shape)
function square:__init()
print("Square constructor")
shape:__init()


end



a = shape()
local v = a:area()
print(v)

b = square()
v = b:area()
print(v)

The output is:
Shape Constructor
100
Square Constructor
Shape Constructor
<crash>

Here's my code in C++:

class shape
{
public:
shape(): x(10), y(10) { cout << "Shape constructor.\n"; }

virtual int area() { return (x*y); }


int x, y;
};

class shape_wrapper: public shape,public luabind::wrap_base
{
public:
shape_wrapper(): shape() { }
virtual int area() { return luabind::call_member<int>(this, "area"); }
static int def_area(shape* p) { return p->shape::area(); }
};

...
module( L )
[
class_<shape, shape_wrapper>("shape")
.def(constructor<>())
.def("area", &shape::area, &shape_wrapper::def_area)
.def_readwrite("x", &shape::x)
.def_readwrite("y", &shape::y)
        ];


I am not sure why it is crashing.

--
Regards,
Emmanuel Barroga


------------------------------------------------------------------------------
ThinkGeek and WIRED's GeekDad team up for the Ultimate
GeekDad Father's Day Giveaway. ONE MASSIVE PRIZE to the
lucky parental unit.  See the prize list and enter to win:
http://p.sf.net/sfu/thinkgeek-promo
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: Derive C++ Class in Lua

Daniel Wallin
On Thu, Jun 24, 2010 at 12:38:24AM -0700, Emmanuel Barroga wrote:
> I am trying to derive my base class in Lua. My code in Lua looks like this:
>
> class 'square' (shape)
> function square:__init()
> print("Square constructor")
> shape:__init()

This should be:

  shape.__init(self)

Ideally this would have failed with some nice error message, but
currently no checking is done on the "self" argument to the constructor.

HTH,
--
Daniel Wallin
BoostPro Computing
http://www.boostpro.com

------------------------------------------------------------------------------
This SF.net email is sponsored by Sprint
What will you do first with EVO, the first 4G phone?
Visit sprint.com/first -- http://p.sf.net/sfu/sprint-com-first
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: Derive C++ Class in Lua

Emmanuel Barroga
Now, I receive the following error:
No matching overload found, candidates:void __init(luabind::argument const&)

On Sun, Jun 27, 2010 at 2:03 PM, Daniel Wallin <[hidden email]> wrote:
On Thu, Jun 24, 2010 at 12:38:24AM -0700, Emmanuel Barroga wrote:
> I am trying to derive my base class in Lua. My code in Lua looks like this:
>
> class 'square' (shape)
> function square:__init()
> print("Square constructor")
> shape:__init()

This should be:

 shape.__init(self)

Ideally this would have failed with some nice error message, but
currently no checking is done on the "self" argument to the constructor.

HTH,
--
Daniel Wallin
BoostPro Computing
http://www.boostpro.com

------------------------------------------------------------------------------
This SF.net email is sponsored by Sprint
What will you do first with EVO, the first 4G phone?
Visit sprint.com/first -- http://p.sf.net/sfu/sprint-com-first
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user



--
Regards,
Emmanuel Barroga


------------------------------------------------------------------------------
This SF.net email is sponsored by Sprint
What will you do first with EVO, the first 4G phone?
Visit sprint.com/first -- http://p.sf.net/sfu/sprint-com-first
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: Derive C++ Class in Lua

Daniel Wallin
On Sun, Jun 27, 2010 at 02:11:43PM -0700, Emmanuel Barroga wrote:
> Now, I receive the following error:
> No matching overload found, candidates:void __init(luabind::argument const&)

Are you sure you used:

  shape.__init(self)
       ^
       Note . not :

--
Daniel Wallin
BoostPro Computing
http://www.boostpro.com

------------------------------------------------------------------------------
This SF.net email is sponsored by Sprint
What will you do first with EVO, the first 4G phone?
Visit sprint.com/first -- http://p.sf.net/sfu/sprint-com-first
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: Derive C++ Class in Lua

Emmanuel Barroga
Thank you, it works now... but aren't the following two equivalent:
b.area(self)
b:area()

The first does not work.

On Sun, Jun 27, 2010 at 2:19 PM, Daniel Wallin <[hidden email]> wrote:
On Sun, Jun 27, 2010 at 02:11:43PM -0700, Emmanuel Barroga wrote:
> Now, I receive the following error:
> No matching overload found, candidates:void __init(luabind::argument const&)

Are you sure you used:

 shape.__init(self)
      ^
      Note . not :

--
Daniel Wallin
BoostPro Computing
http://www.boostpro.com

------------------------------------------------------------------------------
This SF.net email is sponsored by Sprint
What will you do first with EVO, the first 4G phone?
Visit sprint.com/first -- http://p.sf.net/sfu/sprint-com-first
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user



--
Regards,
Emmanuel Barroga


------------------------------------------------------------------------------
This SF.net email is sponsored by Sprint
What will you do first with EVO, the first 4G phone?
Visit sprint.com/first -- http://p.sf.net/sfu/sprint-com-first
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: Derive C++ Class in Lua

Nigel Atkinson-2
Hi Emmanuel,

b.area(self)
and
b:area()

are only the same if b == self, which  is not always the case, and in a constructor they are not.  As far as Lua is concerned, "self" is just a variable. It is not treated specially. You could use "this", or "me", or anything you like instead.

HTH,

Nigel

On Sun, 2010-06-27 at 14:23 -0700, Emmanuel Barroga wrote:
Thank you, it works now... but aren't the following two equivalent:
b.area(self)
b:area()


The first does not work.

On Sun, Jun 27, 2010 at 2:19 PM, Daniel Wallin <[hidden email]> wrote:
On Sun, Jun 27, 2010 at 02:11:43PM -0700, Emmanuel Barroga wrote:
> Now, I receive the following error:
> No matching overload found, candidates:void __init(luabind::argument const&)


Are you sure you used:

 shape.__init(self)
      ^
      Note . not :

--

Daniel Wallin
BoostPro Computing
http://www.boostpro.com

------------------------------------------------------------------------------
This SF.net email is sponsored by Sprint
What will you do first with EVO, the first 4G phone?
Visit sprint.com/first -- http://p.sf.net/sfu/sprint-com-first
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user




--
Regards,
Emmanuel Barroga


------------------------------------------------------------------------------
This SF.net email is sponsored by Sprint
What will you do first with EVO, the first 4G phone?
Visit sprint.com/first -- http://p.sf.net/sfu/sprint-com-first
_______________________________________________ luabind-user mailing list [hidden email] https://lists.sourceforge.net/lists/listinfo/luabind-user


------------------------------------------------------------------------------
This SF.net email is sponsored by Sprint
What will you do first with EVO, the first 4G phone?
Visit sprint.com/first -- http://p.sf.net/sfu/sprint-com-first
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user