Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I think I found the solution. Going through the example I quoted on my ask, and also the boost documentation <a href="http://www.boost.org/doc/libs/1_51_0/libs/python/doc/v2/class.html" rel="nofollow">here</a>, I figured that the problem is that the callback class is the real class being wrapped by Boost.Python, and that it has to be a concrete class. So I implemented in it the missing pure virtual methods:</p> <pre><code>class VAlgorithm_callback: public VAlgorithm { public: VAlgorithm_callback(PyObject *p, const char *name) : self(p), VAlgorithm(name) { } virtual bool Initialize() { return call_method&lt;bool&gt;(self, "Initialize"); } virtual bool Process() { return call_method&lt;bool&gt;(self, "Process"); } virtual bool Finalize() { return call_method&lt;bool&gt;(self, "Finalize"); } const char * GetName() const { return call_method&lt;const char *&gt;(self, "GetName"); } // Supplies the default implementation of GetName static const char * GetName_default(const VAlgorithm&amp; self_) { return self_.VAlgorithm::GetName(); } private: PyObject *self; }; </code></pre> <p>and also modified the wrapper as:</p> <pre><code>class_&lt;VAlgorithm, boost::shared_ptr&lt;VAlgorithm_callback&gt;, boost::noncopyable ("VAlgorithm", init&lt;const char *&gt;()) // .def("Initialize", &amp;VAlgorithm_callback::Initialize) .def("Process", &amp;VAlgorithm_callback::Process) .def("Finalize", &amp;VAlgorithm_callback::Finalize) .def("GetName", &amp;VAlgorithm_callback::GetName_default); </code></pre> <p>Now I can define a child class in Python and call the default GetName() method:</p> <pre><code>&gt;&gt;&gt; class ConcAlg(VAlgorithm): ... pass ... &gt;&gt;&gt; c = ConcAlg("c") &gt;&gt;&gt; c.GetName() 'c' &gt;&gt;&gt; </code></pre>
 

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