title: Cython extension class: How do I expose methods in the auto-generated C struct?
body: <p>I have existing C++ code that defines some classes I need to use, but I need to be able to send those classes to Python code.  Specifically, I need to create class instances in C++, create Python objects to serve as wrappers for these C++ objects, then pass these Python objects to Python code for processing.  This is just one piece of a larger C++ program, so it needs to be done ultimately in C++ using the C/Python API.</p>
  
  <p>To make my life easier, I have used Cython to define extension classes (cdef classes) that serve as the Python wrappers for my C++ objects.  I am using the typical format where the cdef class contains a pointer to the C++ class, which is then initialized when the cdef class instance is created.  Since I also want to be able to replace the pointer if I have an existing C++ object to wrap, I have added methods to my cdef classes to <code>accept()</code> the C++ object and take its pointer.  My other cdef classes successfully use the <code>accept()</code> method in Cython, for example when one object owns another.</p>
  
  <p>Here is a sample of my Cython code:</p>
  
  <p><code>MyCPlus.pxd</code></p>
  
  <pre><code>cdef extern from "MyCPlus.h" namespace "mynamespace":
      cdef cppclass MyCPlus_Class:
          MyCPlus_Class() except +
  </code></pre>
  
  <p><code>PyModule.pyx</code></p>
  
  <pre><code>cimport MyCPlus
  from libcpp cimport bool
  
  cdef class Py_Class [object Py_Class, type PyType_Class]:
      cdef MyCPlus.MyCPlus_Class* thisptr
      cdef bool owned
  
      cdef void accept(self, MyCPlus.MyCPlus_Class &amp;indata):
          if self.owned:
              del self.thisptr
          self.thisptr = &amp;indata
          self.owned = False
  
      def __cinit__(self):
          self.thisptr = new MyCPlus.MyCPlus_Class()
          self.owned = True
  
      def __dealloc__(self):
          if self.owned:
              del self.thisptr
  </code></pre>
  
  <p>The problem comes when I try to access the <code>accept()</code> method from C++.  I tried using the <code>public</code> and <code>api</code> keywords on my cdef class and on the <code>accept()</code> method, but I cannot figure out how to expose this method in the C struct in Cython's auto-generated <code>.h</code> file.  No matter what I try, the C struct looks like this:</p>
  
  <p><code>PyModule.h</code> (auto-generated by Cython)</p>
  
  <pre><code>struct Py_Class {
    PyObject_HEAD
    struct __pyx_vtabstruct_11PyModule_Py_Class *__pyx_vtab;
    mynamespace::MyCPlus_Class *thisptr;
    bool owned;
  };
  </code></pre>
  
  <p>I also tried typing the <code>self</code> input as a <code>Py_Class</code>, and I even tried forward-declaring <code>Py_Class</code> with the <code>public</code> and <code>api</code> keywords.  I also experimented with making <code>accept()</code> a static method.  Nothing I've tried works to expose the <code>accept()</code> method so that I can use it from C++.  I did try accessing it through <code>__pyx_vtab</code>, but I got a compiler error, "invalid use of incomplete type".  I have searched quite a bit, but haven't seen a solution to this.  Can anyone help me?  Please and thank you!</p>