Derived class with shared pointers and .def_readwrite

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

Derived class with shared pointers and .def_readwrite

Nigel Atkinson-2
I think I've found a problem when having a derived classes and using
smart pointers to hold them.

Having:

class A
{
};

class B : public A
{
public:

    int x;
};

and binding with:

    module(L)
    [
        class_< A, shared_ptr<A> >( "A" )
            .def( constructor<>() ),

        class_< B, A, shared_ptr<A> >( "B" )
            .def( constructor<>() )
            .def_readwrite( "x", &B::x )
    ];

then running the Lua code:

b = B()
b.x = 1234

results with the error:

No matching overload found, candidates:
void <unknown>(B&,int const&)

The error disappears if you either:
a) Don't use shared_ptr
b) Don't tell luabind that B is derived from A, i.e. in the second class
binding, remove the base class and change the shared pointer to B.

Oh, and while I remember in the docs in section 8.7 at the end it says
"base" where it should say "derived".  I've attached a patch.


------------------------------------------------------------------------------
The Planet: dedicated and managed hosting, cloud storage, colocation
Stay online with enterprise data centers and the best network in the business
Choose flexible plans and management services without long-term contracts
Personal 24x7 support from experience hosting pros just a phone call away.
http://p.sf.net/sfu/theplanet-com
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user

docs.rst.patch (443 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Derived class with shared pointers and .def_readwrite

Nigel Atkinson-2
My bad.  I figured it out, the base class needed a virtual function, in
order to make in polymorphic.

Nigel

On Thu, 2010-01-28 at 03:12 +1300, Nigel Atkinson wrote:

> I think I've found a problem when having a derived classes and using
> smart pointers to hold them.
>
> Having:
>
> class A
> {
> };
>
> class B : public A
> {
> public:
>
>     int x;
> };
>
> and binding with:
>
>     module(L)
>     [
>         class_< A, shared_ptr<A> >( "A" )
>             .def( constructor<>() ),
>
>         class_< B, A, shared_ptr<A> >( "B" )
>             .def( constructor<>() )
>             .def_readwrite( "x", &B::x )
>     ];
>
> then running the Lua code:
>
> b = B()
> b.x = 1234
>
> results with the error:
>
> No matching overload found, candidates:
> void <unknown>(B&,int const&)
>
> The error disappears if you either:
> a) Don't use shared_ptr
> b) Don't tell luabind that B is derived from A, i.e. in the second class
> binding, remove the base class and change the shared pointer to B.
>
> Oh, and while I remember in the docs in section 8.7 at the end it says
> "base" where it should say "derived".  I've attached a patch.
>
> ------------------------------------------------------------------------------
> The Planet: dedicated and managed hosting, cloud storage, colocation
> Stay online with enterprise data centers and the best network in the business
> Choose flexible plans and management services without long-term contracts
> Personal 24x7 support from experience hosting pros just a phone call away.
> http://p.sf.net/sfu/theplanet-com
> _______________________________________________ luabind-user mailing list [hidden email] https://lists.sourceforge.net/lists/listinfo/luabind-user



------------------------------------------------------------------------------
The Planet: dedicated and managed hosting, cloud storage, colocation
Stay online with enterprise data centers and the best network in the business
Choose flexible plans and management services without long-term contracts
Personal 24x7 support from experience hosting pros just a phone call away.
http://p.sf.net/sfu/theplanet-com
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user