Last active
August 29, 2015 14:16
-
-
Save robertwb/1939f95f579cd8e80505 to your computer and use it in GitHub Desktop.
http://stackoverflow.com/questions/28573479 sample code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef __BaseClass__ | |
#define __BaseClass__ | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string> | |
using namespace std; | |
class BaseClass | |
{ | |
public: | |
BaseClass(){}; | |
virtual ~BaseClass(){}; | |
virtual void SetName(string name){printf("in base set name\n");} | |
virtual float Evaluate(float time){printf("in base Evaluate\n");return 0;} | |
virtual bool DataExists(){printf("in base data exists\n");return false;} | |
}; | |
#endif /* defined(__BaseClass__) */ | |
#ifndef __DerivedClass__ | |
#define __DerivedClass__ | |
#include "BaseClass.h" | |
class DerivedClass:public BaseClass | |
{ | |
public: | |
DerivedClass(){}; | |
virtual ~DerivedClass(){}; | |
virtual float Evaluate(float time){printf("in derived Evaluate\n");return 1;} | |
virtual bool DataExists(){printf("in derived data exists\n");return true;} | |
virtual void MyFunction(){printf("in my function\n");} | |
virtual void SetObject(BaseClass *input){printf("in set object\n");} | |
}; | |
#endif /* defined(__DerivedClass__) */ | |
#ifndef __NextDerivedClass__ | |
#define __NextDerivedClass__ | |
#include "DerivedClass.h" | |
class NextDerivedClass:public DerivedClass | |
{ | |
public: | |
NextDerivedClass(){}; | |
virtual ~NextDerivedClass(){}; | |
virtual void SetObject(BaseClass *input){printf("in set object of next derived class\n");} | |
}; | |
#endif /* defined(__NextDerivedClass__) */ | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# distutils: language=c++ | |
from libcpp cimport bool | |
cdef extern from "Classes.h": | |
cdef cppclass BaseClass: | |
BaseClass() except + | |
void SetName(string) | |
float Evaluate(float) | |
bool DataExists() | |
cdef extern from "Classes.h": | |
cdef cppclass DerivedClass(BaseClass): | |
DerivedClass() except + | |
void MyFunction() | |
float Evaluate(float) | |
# bool DataExists() | |
void SetObject(BaseClass *) | |
cdef extern from "Classes.h": | |
cdef cppclass NextDerivedClass(DerivedClass): | |
NextDerivedClass() except + | |
# *** The issue is right here *** | |
# void SetObject(BaseClass *) | |
cdef class PyBaseClass: | |
cdef BaseClass *thisptr | |
def __cinit__(self): | |
if type(self) is PyBaseClass: | |
self.thisptr = new BaseClass() | |
def __dealloc__(self): | |
if type(self) is PyBaseClass: | |
del self.thisptr | |
cdef class PyDerivedClass(PyBaseClass): | |
cdef DerivedClass *derivedptr | |
def __cinit__(self): | |
self.derivedptr = self.thisptr = new DerivedClass() | |
# def Evaluate(self, time): | |
# return self.derivedptr.Evaluate(time) | |
def SetObject(self, PyBaseClass inputObject): | |
self.derivedptr.SetObject(inputObject.thisptr) | |
cdef class PyNextDerivedClass(PyDerivedClass): | |
cdef NextDerivedClass *nextDerivedptr | |
def __cinit__(self): | |
self.nextDerivedptr = self.derivedptr = self.thisptr = new NextDerivedClass() | |
def SetObject(self, PyBaseClass input): | |
self.nextDerivedptr.SetObject(input.thisptr) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Run as: | |
# python setup.py build_ext --inplace | |
from distutils.core import setup | |
from Cython.Build import cythonize | |
setup( | |
ext_modules = cythonize("*.pyx"), | |
) | |
from inheritTest import PyBaseClass as base | |
from inheritTest import PyDerivedClass as der | |
from inheritTest import PyNextDerivedClass as nextDer | |
#This works now! | |
a = der() | |
b = der() | |
a.SetObject(b) | |
#This doesn't work -- keeping the function declaration causes a overloaded error, not keeping it means the call below works, but it calls the inherited implementation (From derived class) | |
c = nextDer() | |
c.SetObject(b) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment