[英]C++ Struct inheritance in Cython
I'm wrapping a C++ library with cython. 我用cython包装了一个C ++库。 In the headers file, there are some structs that inherit from other structs, like so: 在头文件中,有一些结构是从其他结构继承的,例如:
struct A {
int a;
};
struct B : A {
int b;
};
How should this look in my cdef extern...
block? 在我的cdef extern...
块中应该怎么看?
Using C++ in Cython doesn't mention anything special: 在Cython中使用C ++并没有特别说明:
#file: pya.pyx
cdef extern from "a.h":
cdef cppclass A:
int a
cdef cppclass B(A):
int b
Wrapper class: 包装类:
#file: pya.pyx
cdef class PyB:
cdef B* thisptr
def __cinit__(self):
self.thisptr = new B();
def __dealloc__(self):
del self.thisptr
property a:
def __get__(self): return self.thisptr.a
def __set__(self, int a): self.thisptr.a = a
property b:
def __get__(self): return self.thisptr.b
def __set__(self, int b): self.thisptr.b = b
Example: 例:
import pyximport; pyximport.install(); # pip install cython
from pya import PyB
o = PyB()
assert o.a == 0 and o.b == 0
o.a = 1; o.b = 2
assert o.a == 1 and o.b == 2
To build it you need to instruct pyximport to use c++: 要构建它,您需要指示pyximport使用c ++:
#file: pya.pyxbld
import os
from distutils.extension import Extension
dirname = os.path.dirname(__file__)
def make_ext(modname, pyxfilename):
return Extension(name=modname,
sources=[pyxfilename, "a.cpp"],
language="c++",
include_dirs=[dirname])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.