[C++-sig] overriding a C++ method

Nat Goodspeed ngoodspeed at solidworks.com
Thu Jul 26 01:30:45 CEST 2007


> -----Original Message-----
> From: c++-sig-bounces at python.org [mailto:c++-sig-bounces at python.org]
On
> Behalf Of Francois Ostiguy
> Sent: Wednesday, July 25, 2007 4:35 PM
> To: c++-sig at python.org
> Subject: [C++-sig] overriding a C++ method
> 
> I want to wrap the method do_something, but I would like to avoid
> introducing a converter for std::vector<std::pair<double,int> >.
> Rather, I would like to be able to call do_something(..) in python
> using as an argument, a list of 2-tuples, which would be more natural.
> 
> My first idea was to create a new class AWrapper

[Nat] Another approach, that might sidestep your whole shared_ptr<A>
problem, would be to wrap specific methods (do_something() and others)
rather than wrapping the entire class A.

In our Boost.Python extension module, we define a class Drawable with a
method:

    virtual void addRotation(double angle, const Vector3& axis);

Our Vector3 class is not published to Python; instead we want to accept
any Python triple. So we define the following:

static void Drawable_addRotation(Drawable& drawable, double angle,
object axis)
{
    drawable.addRotation(angle, PVector3(axis));
}

where PVector3 is defined (in the same module) as follows:

/** conversion from (x, y, z) to Vector3 */
struct PVector3: public Vector3
{
    PVector3(boost::python::object tuple):
        Vector3(boost::python::extract<double>(tuple[0]),
                boost::python::extract<double>(tuple[1]),
                boost::python::extract<double>(tuple[2])) {}
};

Then when we publish Drawable::addRotation, we pass
&Drawable_addRotation rather than &Drawable::addRotation.

The above could probably be streamlined further by registering a
converter. My point is simply that you can wrap a method using a free
function if it solves a problem for you.



More information about the Cplusplus-sig mailing list