[C++-sig] pointers and pyste
dave at boost-consulting.com
Mon Apr 28 00:44:43 CEST 2003
Sameer Agarwal <sagarwal at cs.ucsd.edu> writes:
> In the above function I called the virtual method intersect for each
> of the objects in the list object_list and pass it a reference to the
> class calling the method.
> when I use boost to wrap the sphere class, the ray which is passed to
> Sphere::intersect has a different address than the one which actually
> calls the routine.
> which I am guess is because somewhere along the way instead of a
> reference a copy of the object is being passed.
Yeah, what happens is that in:
call_method<R>(self, "name", x, y, z)
which is what gets invoked in the Boost.Python override of any virtual
member function called name, x, y, and z are copied, even if they're
references (C++ actually gives us no way to tell whether they're
references or not). In order to get pass-by-reference semantics you
self, "name", boost::ref(x), boost::ref(y),
Pass-by-reference is not the default because it's dangerous: someone
could destroy the referent while you still have a Python object alive
that's referencing it. If you have the luxury of modifying your
interfaces, try passing around boost::shared_ptr<T> instead of T& or T
More information about the Cplusplus-sig