简体   繁体   English

使用 Cython 包装 C++ 类时处理指针

[英]Handling pointers when wrapping C++ class with Cython

I'm having trouble when handling pointers with cython.使用 cython 处理指针时遇到问题。 The cython implementation of the class holds a pointer to a C++ instance of class Person .该类的 cython 实现持有一个指向类Person的 C++ 实例的指针。 Here's my .pyx file:这是我的.pyx文件:

person.pyx人.pyx

cdef class PyPerson:
    cdef Person *pointer

    def __cinit__(self):
        self.pointer=new Person()

    def set_parent(self, PyPerson father):
        cdef Person new_father=*(father.pointer)
        self.c_person.setParent(new_father)        

The C++ method setParent takes a Person object as an argument. C++ 方法setParentPerson对象作为参数。 Since the attribute pointer of the PyPerson class is a pointer to a Person object, I thought that I could get the object at the adress pointed by *pointer with the syntax *(PyPersonObject.pointer) .由于PyPerson类的属性pointer是指向Person对象的指针,我认为我可以使用*(PyPersonObject.pointer)语法在*pointer的地址处获取对象。 However when I try to compile it I get the following error但是,当我尝试编译它时,出现以下错误

 def set_parent(self, PyPerson father):
    cdef Person new_father=*(father.pointer)
                             ^
------------------------------------------------------------

person.pyx:51:30: Cannot assign type 'Person *' to 'Person'

Does someone knows how can I get to the object at the adress of the pointer?有人知道如何到达指针地址处的对象吗? When I do the same in a C++ program I get no error.当我在 C++ 程序中做同样的事情时,我没有得到任何错误。 Here's the implementation of the C++ class in case you want to see it :这是 C++ 类的实现,以防您想查看它:

person.cpp人.cpp

Person::Person():parent(NULL){
}

Person::setParent(Person &p){
     parent=&p;
}

NOTE: I can't solve it by holding a Person instance ( cdef Peron not_pointer ) for other reasons involving the complete class.注意:由于涉及完整类的其他原因,我无法通过持有Person实例( cdef Peron not_pointer )来解决它。

I should have read the entire cython docs on using C++ with Cython.我应该阅读有关在 Cython 中使用 C++ 的整个 cython 文档。 For those who don't know, the dereference operator * can't be used in Cython.对于那些不知道的人,在 Cython 中不能使用取消引用运算符* Instead you need to import dereference from the cython.operator module.相反,您需要从cython.operator模块导入dereference When you want to access the object at the pointed address, you should write dereference(pointer) .当您想访问指向地址处的对象时,您应该编写dereference(pointer)

In concrete, the answer to my problem is to write cdef Person new_father=dereference(father.c_person) . cdef Person new_father=dereference(father.c_person) ,我的问题的答案是写cdef Person new_father=dereference(father.c_person)

One another solution to this might be to index into the pointer at location 0 to dereference a pointer in Cython.对此的另一种解决方案可能是索引位置0处的指针以取消引用 Cython 中的指针。 For example, suppose we have a golden_ratio C double and a p_double C pointer:例如,假设我们有一个 Golden_ratio C double 和一个 p_double C 指针:

cdef double golden_ratio
cdef double *p_double

We can assign golden_ratio's address to p_double using the address-of operator, &:我们可以将golden_ratio's地址p_double使用地址的操作,:

p_double = &golden_ratio

We can now assign to golden_ratio through p_double using our indexing-at-zero-to-dereference syntax:我们现在可以使用我们的indexing-at-zero-to-dereference语法通过p_double分配给golden_ratio

p_double[0] = 1.618
print(golden_ratio)
# => 1.618

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM