[C++-sig] Problems with custom smart pointers and multiple inheritance
Alex Mohr
amohr at pixar.com
Tue Mar 21 18:55:55 CET 2006
> print c1.PassAPointer(c1) # fails
> Boost.Python.ArgumentError: Python argument types in
> MyBaseA.PassAPointer(MyClass, MyClass) did not match C++ signature:
> PassAPointer(class MyNamespace::MyBaseA {lvalue}, class
> MyNamespace::MyPointer<class MyNamespace::MyBaseA>)
Have you tried using MyPointer<T> as the held types? That is:
class_<MyBaseA, MyPointer<MyBaseA>, boost::noncopyable>
class_<MyBaseB, MyPointer<MyBaseB>, boost::noncopyable>
class_<MyClass, MyPointer<MyClass>, boost::noncopyable, bases<...> >
Then you shouldn't need register_ptr_to_python (only when you get to
polymorphism and the classes you are wrapping are really wrapper<T>).
> * I need to know the PyObject wrapping my C++ class instance, but the
> "class syntax #2" invoking the constructor taking a PyObject as
> parameter works even worse.
If you read the docs for class_ where it talks about HeldType semantics,
you'll see info regarding 'has_back_reference'. That may work for you.
> * The Python object returned by 'GetBaseA()' is not the same as the
> calling Python object although they are actually referencing the same
> C++ class instance. Is there any way to avoid this ? (this is not the
> biggest of my problems though)
If you use boost::shared_ptr, it sort of works. With any other smart
pointer you can't really do it as far as I know. There's some ongoing
discussion about this. I would like to fix it.
By the way, injecting constructors via make_constructor is not going to
work for you if/when you try to expose these classes as polymorphic to
python. This is because make_constructor doesn't seem to cause
boost::python::detail::initialize_wrapper() to be called. Seems like it
maybe could/should. I've cobbled together my own make_constructor in
order to work around this, so if you run into that, perhaps I can help.
Alex
More information about the Cplusplus-sig
mailing list