[C++-sig] Re: Containers for derived classes

abeyer at uni-osnabrueck.de abeyer at uni-osnabrueck.de
Fri Aug 8 16:56:30 CEST 2003


> abeyer at uni-osnabrueck.de writes:
>
>> Hence, a and a1 are not the same Python objects, but the same C++ objects.
>> Thus, sub-classing A still doesn't work:
>
> Which version of Boost.Python are you using?  I wouldn't expect it to
> work with any released version.

Well, now I have got a CVS version from today (08/08) - downloaded from
anonymous at cvs.boost.sourceforge.net:/cvsroot/boost, so it may be 24 hrs
old.
Still, I am having the same trouble:

>>> from ptr_test import *
>>> a=A(0)
>>> c=C()
>>> c.set(a)
>>> a1=c.get()
>>> a
<ptr_test.A object at 0x008F7750>
>>> a1
<ptr_test.A object at 0x008F68B0>
>>>

I.e. the identity is not preserved. Could this be a problem with MinGW,
which I am using?
To be sure, here is the complete code again:

#include <boost/shared_ptr.hpp>
#include <boost/python.hpp>

using namespace boost;

class A {
     public:
	A(int i) : val(i) { }
	int val;
};
typedef shared_ptr<A> A_ptr;

class C {
     public:
	void set(A_ptr a) { this->a=a; }
	A_ptr get() { return this->a; }
	void f() { a.get()->val *= 2; }
     private:
	 A_ptr a;
};

BOOST_PYTHON_MODULE(ptr_test)
{
    using namespace boost::python;
    class_<A, A_ptr>("A", init< int >() )
	.def_readwrite("val", &A::val)
    ;
    class_<C>("C" )
	.def("set", &C::set )
        .def("get", &C::get )
        .def("f",   &C::f )
    ;
}






More information about the Cplusplus-sig mailing list