2009-12-02 19 views
5

Bir sınıf verildiğinde:Boost.Python: Bir sınıfın dışındaki bir kurucunun tanımlanması

class TCurrency { 
    TCurrency(); 
    TCurrency(long); 
    TCurrency(const std::string); 
    ... 
}; 

Boost ile sarılmış.Python:

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

Python'da bir kurucu olarak görünen bir fabrika yöntemi oluşturmak mümkün mü?

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

python'da:

bar = TCurrency(foo) 

cevap

12

için make_constructor (denenmemiş) kullanabilir:

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

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

argüman make_constructor sarılmış sınıf için bir işaretçi [1] döndüreceği funktoru olup.

[1] Aslında, işlev, bir işaretçi tutucu türünü döndürmelidir; bu nedenle, işaretçi tutucunuz boost::shared_ptr ise, işlev, bir ham işaretçiden ziyade bir boost :: shared_ptr döndürmelidir.

0

my example olabilir. - init_python_object işlevi ihtiyaç duyduğunuz parametreleri alabilir. Basit not: class_t'yi boost::noncopyable and no_init ile tanımlarım.

İlgili konular