Calling C++ Parent / Base class Constructors #1581

Open
scopatz opened this Issue Jan 17, 2017 · 0 comments

Projects

None yet

1 participant

@scopatz
scopatz commented Jan 17, 2017

There didn't seem to be an issue for this, but I wanted to capture this thread from the mailing list. Basically, cppclass cannot call the base class constructors. This is must for some libraries where "Base::Base(arg)" does cannot be called explicitly in the constructor body.

The example from the mailing list is below.

For the time being I have circumvented this via a small post-process scrip available here: https://github.com/scopatz/cyclus/blob/pyagent/cli/post-process-cython.py


myclass.hpp

namespace mine {

struct myclass
{
	//myclass();
	myclass(int a_, int b_);
	int a;
	int b;
};

}

myclass.cpp

//myclass::myclass() {}

myclass::myclass(int a_, int b_)
{
	this->a = a_;
	this->b = b_;
}

cmyclass.pxd:

cdef extern from "myclass.hpp" namespace "mine":

        cdef cppclass myclass:
        #myclass()
        myclass(int a_, int b_)
        int a
        int b

myclass_wrapper.pyx

cimport cmyclass


cdef cppclass _myclass(cmyclass.myclass):
    __init__(PyObject* tup):
        
       # WOULD LIKE TO CALL myclass::myclass(int, int) HERE.

        this.a = int((<object>tup)[0])
        this.b = int((<object>tup)[1])


cdef class MyClass(object):

    cdef _myclass *thisptr

    def __cinit__(self):
        self.thisptr = new _myclass((5, 5))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment