[C++-sig] custom constructor problems
Ralf W. Grosse-Kunstleve
rwgk at yahoo.com
Mon Dec 15 17:58:07 CET 2008
I think this is related:
http://mail.python.org/pipermail/cplusplus-sig/2006-March/010092.html
http://mail.python.org/pipermail/cplusplus-sig/2006-March/010095.html
I gave up on the obj1 == obj2 comparison idea and added a
..memory_id() method, which returns the memory location (cast to Python int).
----- Original Message ----
From: Renato Araujo <renatox at gmail.com>
To: Development of Python/C++ integration <cplusplus-sig at python.org>
Sent: Monday, December 15, 2008 6:17:16 AM
Subject: [C++-sig] custom constructor problems
Hi again :D,
To solve my old problem, I'm trying create custom constructor which
return a shared_ptr<MyClass> like that code:
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
class my_class_wrapper : my_class, python::wrapper<my_class>
{
.......
}
shared_ptr<my_class_wrapper> _create_object(parent *p)
{
return shared_ptr<my_class_wrapper>(new my_class_wrapper(p),
_my_delete_function);
}
shared_ptr<my_class_wrapper> _create_object2()
{
return shared_ptr<my_class_wrapper>(new my_class_wrapper(0),
_my_delete_function);
}
class_<my_class_wrapper>("MyClass", python::no_init)
.def("__init__", python::make_constructor(_create_object))
.def("__init__", python::make_constructor(_create_object2))
.def("get_parent", &MyClass:getParent,
return_value_policy<reference_existing_object>());
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
but during my python test, the function get_parent not return the same
python object created in the object constructor. my test is:
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
parent = MyClass()
child = MyClass(parent)
if (child.get_parent() != parent)
print "FAIL"
else
print "OK"
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Always I got the "FAIL" message, but if I remove the custom
constructor the test pass. What I doing wrong all my classes inherit
from "python::wrapper<X>". Then I think all classes have implicit
conversion to "shared_ptr" correct?
BR
--
Renato Araujo Oliveira Filho
_______________________________________________
Cplusplus-sig mailing list
Cplusplus-sig at python.org
http://mail.python.org/mailman/listinfo/cplusplus-sig
More information about the Cplusplus-sig
mailing list