Derived classes and registration with shared_ptr holder

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view

Derived classes and registration with shared_ptr holder

Alexander Gladysh
Hi, all!

Say, I have this:

  typedef boost::shared_ptr<Base> BasePtr;
  class Base : boost::noncopyable
    static BasePtr Create() { return BasePtr(new Base); }
    virtual void foo() { print("Base::Foo()\n"); }
    virtual ~Base() { }

    Base() { } // Allow creation only by class factory or child class.

  void PolymorphUse(const BasePtr & ptr);

  typedef boost::shared_ptr<Derived> DerivedPtr;
  class Derived : public Base
    static DerivedPtr Create() { return DerivedPtr(new Base); }
    virtual void foo() { print("Derived::Foo()\n"); }
    void bar() { print("Derived::Bar()\n"); }

    Derived() { } // Allow creation only by

I want classes Base and Derived to be exposed to Lua. Following doesn't work:

  luabind::class<Base, BasePtr>("Base_t")
    .def("foo", &Base::foo)
  , luabind::def("Base", &Base::Create)
  , luabind::def("PolymorphUse", &PolymorphUse)
  , luabind::class<Derived, Base, BasePtr>("Derived_t")
    .def("foo", &Derived::foo)
    .def("bar", &Derived::bar)
  , luabind::def("Derived", &Derived::Create)

The DerivedPtr type doesn't gets registered in Lua, and
Derived::Create registration fails in runtime.

If I change return type of Derived::Create to BasePtr, then I
(obviously) do not get access to Derived::bar in Lua.

If I (as opposed to Luabind manual) register Derived with DerivedPtr holder,

  , luabind::class<Derived, Base, DerivedPtr>("Derived_t")

then I can't use functions, which accept base class pointers with
derived class poiners (like PolymorphUse). Luabind says that it can't
convert one pointer to another.

Am I misunderstanding something in the way shared pointer holders work
in Luabind?

Thanks in advance,

Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
<a href="">
luabind-user mailing list
[hidden email]