[C++-sig] Wrappend objects identity problem
Victor Nakoryakov
nail-mail at mail.ru
Thu Mar 30 11:31:13 CEST 2006
Victor Nakoryakov wrote:
> Ok, I think my previous post was too complicated, here is simplified
> version:
>
>
>>class A
>>{
>>};
>>
>>boost::shared_ptr<A> ptr;
>>boost::shared_ptr<A> getA()
>>{
>> if (!ptr)
>> ptr = boost::shared_ptr<A>(new A);
>>
>> return ptr;
>>}
>>
>>void Init()
>>{
>> python::class_< A, boost::shared_ptr<A>, boost::noncopyable >( "AClass", python::no_init );
>> python::def("getA", &getA);
>>}
>
>
> And py code is:
>
>
>>a1 = getA()
>>a2 = getA()
>>l = [a1, a2]
>>print a1
>>print a2
>>print getA() in l
>>print a1 is a2
>>print a1 == a2
>
>
> Output is:
>
> <_mymodule.AClass object at 0x017DB070>
> <_mymodule.AClass object at 0x017DB0B0> <- another address!
> False
> False
> False
>
> The question is the same: What I have to do to have the same address of
> AClass every time I call getA() from python.
>
> I can't find solution already for 3 days. Have anyone ideas?
>
Furtermore following C++ code:
> class A
> {
> };
>
> A* ptr = 0;
> A* getA()
> {
> if (!ptr)
> ptr = new A;
>
> return ptr;
> }
>
> void Init()
> {
> python::class_< A, boost::noncopyable >( "AClass", python::no_init );
> python::def("getA", &getA, python::return_value_policy<python::reference_existing_object>());
> }
Produces the same output! What's wrong?
--
Victor (aka nail) Nakoryakov
nail-mail<at>mail<dot>ru
Krasnoznamensk, Moscow, Russia
More information about the Cplusplus-sig
mailing list