Cythonのデフォルトコンストラクターの使用に問題があります。
私のC++クラスノードは次のとおりです
Node.h
class Node
{
public:
Node()
{
std::cerr << "calling no arg constructor" << std::endl;
w=0.0;
d=0.0;
}
Node(double val, double val2);
{
std::cerr << "calling 2 args constructor" << std::endl;
this->w=val;
this->d=val2;
}
private:
double d,w;
}
次のようにCythonでラップされています
cdef extern from "Node.h":
cdef cppclass Node:
Node() except +
Node(double val1, double val2) except +
double d
double w
cdef class pyNode:
cdef Node *thisptr # hold a C++ instance which we're wrapping
def __cinit__(self):
self.thisptr = new Node()
def __cinit__(self, double val1, double val2):
self.thisptr = new Node(val1,val2)
def __dealloc__(self):
del self.thisptr
def __repr__(self):
return "d=%s w=%s" % (self.thisptr.w, self.thisptr.w )
Cythonコードはうまくコンパイルされますが、特にPythonから呼び出された場合
from pyNode import pyNode as Node
n=Node(1.0,2.0)
期待されるcalling 2 args constructor
文字列を取得しましたが、「引数なし」コンストラクターを使用してPythonからNodeオブジェクトを宣言しようとすると(__cinit__(self)
出力が得られないため、正しく宣言する必要があります。これは、引数なしコンストラクターが呼ばれていません!
ラップされたクラスのcinitメソッドから明示的に呼び出すにはどうすればよいですか?