[C++-sig] boost.python, how to have python transfer ownership to a c++ object

Baptiste Lepilleur gaiacrtn at free.fr
Tue Jan 18 10:22:33 CET 2005


    I'm trying to expose a method that take ownership of the passed
parameter (as if the parameter was an auto_ptr):

---
    struct Item {    // polymorphic class (may be subclassed)
    };

    struct ListBox
    {
        void addItem( Item *item )  // take ownship of 'item'
        {
            item_.reset( item );
        }

    private:
        std::auto_ptr<Item> item_;
    };
---
    And use it like this in python:

    class MyItem(Item):
        pass
    list = ListBox()
    list.addItem( MyItem() )

    The issue is that the MyItem instance is owned by python and get
destroyed by python after the addItem statement. How can I indicate that the
'item' parameter passed to addItem() is now owned by the C++ instance of
'list' ?

    Baptiste.




More information about the Cplusplus-sig mailing list