Re: How do I register an abstract class with luabind?
Daniel Wallin <[email protected]>
| Newsgroups | gmane.comp.lang.lua.bind.user |
|---|---|
| Message-ID | <[email protected]> |
mat buckland wrote:
> How do I register a class that contains one or more pure virtual methods?
It depends on whether or not you want to derive from the class and
override the functions in Lua. If not, you just export them as usual.
class X
{
public:
virtual ~X() {}
virtual void f() = 0;
};
...
class_<X>("X")
.def("f", &X::f)
If you want to be able to override them in Lua, you need to add a
wrapper class:
class X_wrapper : public X, public luabind::wrap_base
{
public:
void f()
{
call<void>("f");
}
};
...
class_<X, X_wrapper>("X")
.def(constructor<>()) // 1
.def("f", &X::f)
Note that you don't need to add a "default_f()" function to the wrapper
type. This isn't mentioned in the docs, but it isn't needed in this case
since there is no default implementation. Attempting to call "f()" when
there is no override will give an "attempt to call nonexistant function"
error.
Note also that you need to make the class constructible (1). This is
needed so that a derived Lua class can call the base __init() function.
--
Daniel Wallin
BoostPro Computing
http://www.boostpro.com
------------------------------------------------------------------------------
Return on Information:
Google Enterprise Search pays you back
Get the facts.
http://p.sf.net/sfu/google-dev2dev