C++ Python:Grab';自我';从成员函数

C++ Python:Grab';自我';从成员函数,c++,python,boost,C++,Python,Boost,Python中的类成员函数必须显式声明表示类实例的self参数。通过使用Booost?,有没有办法从C++中获得自我>代码>? class FooBar { public: void func() { } }; // A wrapper for the above class struct FooBar_W : public FooBar { void func(boost::python::object self) { // Do smth

Python中的类成员函数必须显式声明表示类实例的
self
参数。通过使用Booost?

,有没有办法从C++中获得<代码>自我>代码>?
class FooBar
{
  public:
    void func() {
    }
};

// A wrapper for the above class
struct FooBar_W
    : public FooBar
{
    void func(boost::python::object self) {
        // Do smth with `self`
        FooBar::func();
    } 
};

BOOST_PYTHON_WRAPPER(module)
{
    class_<FooBar_W>("FooBar")
        .def("func", &FooBar_W::func)
     ;
}
经理需要弄清楚我刚才排队的是哪种类型的事件。我想我应该做一些事情,比如“C++ >类型(事件)”。通过这种方式,我可以确定事件的类型并知道要通知哪些函数。我想在C++中获得<代码>自我>代码,这样我就可以访问它的类型<代码>
我可以让脚本编写者手动编辑保存类型名称的新字段,但为什么?信息已经存在(<代码>·γ- NAMEYOG/< C++ >属性),为什么要复制它,但更重要的是,为什么要用实现细节来打扰脚本?
您可以想到行
FooBar::func()转换为
static\u cast(this)->func()
是可行的。方法可以在下面的链接中找到;该页面记录了一种公开纯虚拟函数的方法(旧方法)。不过,该示例可以适应其他需要。

>

这是一个老问题,但对于那些仍在寻找合理简单解决方案的人来说:

静态函数(非成员和成员)接收一个
const boost::python::object&self
作为第一个参数。因此,您可以执行以下操作:

class FooBar
{
  public:
    static void func(const boost::python::object self) {
        FooBar& thisref = boost::python::extract<FooBar&>(self)();
        // use self as well as thisref
    }
};

};

BOOST_PYTHON_WRAPPER(module)
{
    class_<FooBar>("FooBar")
        .def("func", &FooBar::func)
     ;
}
class FooBar
{
公众:
静态void func(const boost::python::object self){
FooBar&thisref=boost::python::extract(self)();
//使用self和thisref
}
};
};
BOOST_PYTHON_包装器(模块)
{
类(“FooBar”)
.def(“func”,&FooBar::func)
;
}

<代码>我想要Python对象本身,而不是C++对象。我也没有看到你的自我参数。你能发布你的python客户端代码吗?我仍然怀疑你不需要通过赛尔夫考试。与中一样,我认为您已经在
中拥有了python对象
class FooBar
{
  public:
    static void func(const boost::python::object self) {
        FooBar& thisref = boost::python::extract<FooBar&>(self)();
        // use self as well as thisref
    }
};

};

BOOST_PYTHON_WRAPPER(module)
{
    class_<FooBar>("FooBar")
        .def("func", &FooBar::func)
     ;
}