user1589028
user1589028

Reputation: 61

getting a reference to self when using boost::python

I am trying to port a Python class to C++ using boost::python with the hope of speeding up the execution of a Python application (the class I am porting to C++ is responsible for ~30% of the applications execution time).

The init of the original Python class looks like:

class PyClass(object):
    def __init__(self, child):
        child.set_parent(self)
        ...

How do I replicate this in a C++ constructor?

if I have a C++ class:

class CClass
{
    // to get input args that match the Python class I need
    CClass(boost::python::object &child)
    {
         // but how do I get the boost::python::object self
         // as I only have *this in C++ ?
         CClass& c = boost::python::extract<CClass&>(child);
         c.set_parent(self);
    }

    ...
}

Thanks, Mark

Upvotes: 5

Views: 925

Answers (1)

James
James

Reputation: 25543

You can use the this pointer via boost::python::ptr(this), as described in this answer.

Upvotes: 3

Related Questions