Skip to content

Instantly share code, notes, and snippets.

@hainm
Last active March 27, 2017 08:35
Show Gist options
  • Save hainm/7d225e03726c2a24c528 to your computer and use it in GitHub Desktop.
Save hainm/7d225e03726c2a24c528 to your computer and use it in GitHub Desktop.
test.pyx
# distutils: language = c++
cdef cppclass A:
void c_test():
print ("hello from A")
cdef cppclass B(A):
void SetObject(A* ptr):
print ("dummy method, hello from B")
cdef class pyA:
cdef A* thisptr
def __cinit__(self):
self.thisptr = new A()
def __dealloc__(self):
if self.thisptr is not NULL:
del self.thisptr
cdef class pyB (pyA):
def __cinit__(self):
if self.thisptr:
del self.thisptr
self.thisptr = new B()
def __dealloc__(self):
if self.thisptr is not NULL:
del self.thisptr
def set_object(self, obj):
"""
>>> from test import pyA, pyB
>>> pya = pyA()
>>> pyb = pyB()
>>> pyb_2 = pyB()
>>> pyb_2.set_object(pya)
dummy method, hello from B
>>> pyb_2.set_object(pyb)
dummy method, hello from B
"""
cdef pyA tmpobj = <pyA> obj
# still need to cast to B class to use SetObject
(<B*> self.thisptr).SetObject(<A*> tmpobj.thisptr)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment