【发布时间】:2017-03-02 11:44:22
【问题描述】:
我在使用 cython 处理指针时遇到问题。类的 cython 实现包含一个指向类 Person 的 C++ 实例的指针。这是我的.pyx 文件:
person.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)
C++ 方法setParent 将Person 对象作为参数。由于PyPerson 类的属性pointer 是指向Person 对象的指针,因此我认为可以使用*(PyPersonObject.pointer) 语法在*pointer 指向的地址处获取对象。但是,当我尝试编译它时,出现以下错误
def set_parent(self, PyPerson father):
cdef Person new_father=*(father.pointer)
^
------------------------------------------------------------
person.pyx:51:30: Cannot assign type 'Person *' to 'Person'
有人知道我怎样才能到达指针地址处的对象吗? 当我在 C++ 程序中做同样的事情时,我没有收到任何错误。如果您想查看它,这是 C++ 类的实现:
person.cpp
Person::Person():parent(NULL){
}
Person::setParent(Person &p){
parent=&p;
}
注意:由于涉及整个类的其他原因,我无法通过持有 Person 实例 (cdef Peron not_pointer) 来解决它。
【问题讨论】:
标签: python c++ pointers cython