[C++-sig] virtual method issue ?
Roman Yakovenko
roman.yakovenko at gmail.com
Thu Mar 15 21:10:54 CET 2007
On 3/15/07, Adrien Saladin <adrien-ml at wizzi.net> wrote:
>
> Dear all,
>
> I'm exposing a C++ library to python using boost python.
> Almost everything works fine except this:
>
> I have a Minimizer class that needs a "function" object to minimize. These
> objects communicates with std::vector<double> and I think the problem is
> around there...
>
> I had to (partially) expose std::vector<double> to python because of a
> lvalue
> convertor problem.
>
> The function object is derived from an abstract class "Base".
> When I try to redefine the function in python, the std::vector doesn't
> seem to
> be correctly transmitted between objects.
>
> You will find bellow the minimal example I found to reproduce the
> undesired
> feature...
> Any advice would be really appreciated !
Your code contains one error and few places where better code could be
created:
1. Minimizer(Base& base) - when you expose this constructor you'd better
use call policies, otherwise
you will have to keep base instance somewhere
And the error:
class PyDerived(Base):
def Function(self, v, grad):
grad[0]=45.0
return 0
You have to call __init__ method of Base class
class PyDerived(Base):
def __init__( self ):
Base.__init__( self )
def Function(self, v, grad):
grad[0]=45.0
return 0
This should fix the assert you get.
P.S. This error comes too often. Is it possible to add an exception, which
will give a hint to a user what should be done?
Thanks
--
Roman Yakovenko
C++ Python language binding
http://www.language-binding.net/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20070315/6fd13ac2/attachment.htm>
More information about the Cplusplus-sig
mailing list