Passing C++ pointer as argument into Cython function

后端 未结 4 2096
萌比男神i
萌比男神i 2021-02-04 00:36
cdef extern from \"Foo.h\":
    cdef cppclass Bar:
        pass

cdef class PyClass:
    cdef Bar *bar

    def __cinit__(self, Bar *b)
        bar = b

4条回答
  •  长情又很酷
    2021-02-04 01:26

    As of Cython 0.21 it has been possible to declare cdef methods with the @staticmethod decorator. This allows static creator methods that take non-Python arguments:

    cdef extern from "Foo.h":
        cdef cppclass Bar:
            pass
    
    cdef class PyClass:
        cdef Bar *bar
    
        @staticmethod
        cdef create(Bar *bar):
            cdef PyClass pc = PyClass()
            pc.bar = bar
            return pc
    

提交回复
热议问题