2009-12-02 10 views
5

をコンストラクタの定義:Boost.Python:クラス外でクラスを考える

Boost.Pythonに包まれた
class TCurrency { 
    TCurrency(); 
    TCurrency(long); 
    TCurrency(const std::string); 
    ... 
}; 

class_<TCurrency>("TCurrency") 
    .def(init<long>) 
    .def(init<const std::string&>) 
    ... 
    ; 

はコンストラクタとして表示されるファクトリメソッドを作成することは可能ですPythonで:

TCurrency TCurrency_from_Foo(const Foo&) { return TCurrency(); } 

ようにPythonで:

bar = TCurrency(foo) 

答えて

12

あなたはmake_constructor(未テスト)を使用することができ:

TCurrency* TCurrency_from_Foo(const Foo&) { return new TCurrency(); } 

class_<TCurrency>("TCurrency") 
    .def("__init__", boost::python::make_constructor(&TCurrency_from_Foo)) 
; 

引数をmake_constructorには、ラップされたクラスへのポインタ[1]を返す任意のファンクタです。

[1]実際には、関数はポインタホルダ型を返す必要があります。したがって、ポインタホルダがboost::shared_ptrの場合、関数は生ポインタの代わりにboost :: shared_ptrを返さなければなりません。

0

my exampleが役に立ちます - init_python_object関数は、必要な任意のパラメータを取ることができます。シンプルノート:私はboost::noncopyable and no_initでclass_tを定義します。

関連する問題