Boost logo

Boost Users :

From: Ben Hutchings (ben.hutchings_at_[hidden])
Date: 2005-01-19 08:53:43


Benjamin Schmeling wrote:

>Hi,
>
>I am wrapping a class "bigint" from c++ to python. I have overloaded the add
>method to do something like this on python:
>
>>>>n=bigint(5);
>>>>n+5L
>>>>
>5
>
>
Not 10?

>This works well, but if I try to do it vice versa my overloaded add method
>isn't called by python:
>
>>>>n=bigint(5);
>>>>5L+n
>>>>
>Causes error.
>
>My add methods look like this:
>bigint add_bigint_l(bigint const& b, long_ const& l)
>bigint add_l_bigint(long_ const& l,bigint const& b)
>
>and are defined by
>
>.def("__add__", add_bigint_l)
>.def("__add__", add_l_bigint)
>
>Can someone tell me howto realize it?
>
Remember that Python doesn't support overloading within a class. So it
evaluates a + b as a.__add__(b) or if that fails b.__radd__(a), with the
"r" indicating that the arguments are reversed. I believe you should
change the second line to:

    .def("__radd__", add_bigint_l)

and that there is no need for add_l_bigint.

Ben.


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net