Note that there are some explanatory texts on larger screens.

plurals
  1. POcython wrapping of base and derived class
    primarykey
    data
    text
    <p>I am trying to wrap C++ Base and Derived classes and Base is an abstract class. (I have editted this to make this example SSCCE). The Base class defines the interface, I donot like to repeat them in the Derived Python class (not sure is it possible). But I do not know how to tell Cython, Derived is derived from Base. I have tried several ways, but did not succeed.</p> <pre><code>#include &lt;string&gt; class Base { std::string name_; public: Base (const std::string&amp; name): name_(name) {} std::string name() const { return name_; } void setName(const std::string&amp; name) { name_ = name; } virtual double length() const = 0; virtual ~Base() {} }; class Derived: public Base { double len_; public: Derived(const std::string&amp; name, double len) : Base(name), len_(len){ } virtual double length() const { return len_; } void setLength(double len) { len_ = len; } }; </code></pre> <p>I am trying to use Cython wrap these two classes:</p> <pre><code>from libcpp.string cimport string cdef extern from "base.h": cdef cppclass c_Base "Base": string name_ string name() void setName(const string&amp; name) cdef extern from "base.h": ctypedef struct c_Derived "Derived": double len_ double length() void setLength(double v) c_Derived *new_Derived "new Derived" (const string&amp; name, double len) void del_Derived "delete" (c_Derived *p) from tlib cimport c_Derived cdef class Base: cdef c_Base *thisptr def __cinit__(self, const string&amp; name): pass property name: def __get__(self): return self.thisptr.name() def __set__(self, const string&amp; v): self.thisptr.setName(v) cdef class Derived: cdef c_Derived *thisptr # hold a C++ instance which we're wrapping def __cinit__(self, const string&amp; name, double len): self.thisptr = new_Derived(name, len) def __dealloc__(self): del_Derived(self.thisptr) property length: def __get__(self): return self.thisptr.length() def __set__(self, double v): self.thisptr.setLength(v) </code></pre> <p>The setup.py is like this:</p> <pre><code>from distutils.core import setup from distutils.extension import Extension from Cython.Distutils import build_ext setup( name = 'pytlib', ext_modules=[ Extension("tlib", sources=["tlib.pyx"], include_dirs=["."], language="c++"), ], cmdclass = {'build_ext': build_ext}, ) </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload