c++ - Base and derived classes in Boost.Python -
i have 2 classes - lets , b. class b child class of , both have function h, such that:
class a{ public: a(){;;} // ctor virtual ~a(){ ;; } // destr. virtual double h(int i,int j){ std::cout<<"calling base function h. level abstract\n"; exit(0); return 0.0; }// h };// class
the derived class b is:
class b : public a{ public: b(){;;} // ctor ~b(){ ;; } // destr. double h(int i,int j){ return 1.0; }// h };// class b
i have function takes arguments of class type:
double f(a x){ return x.h(1,1); }
i want use object of derived type b argument function in python. so, export 2 classes , function explained here:
boost_python_module(modab){ class_<a>("a", no_init); class_<b, bases<a> >("b", init<>()) .def("h", &b::h) ; def("f", &f); }
so in python do:
>>> modab import * >>> x = b() >>> res = f(x)
as output see method h called base class, not derived class expected:
calling base function h. level abstract
so, question i'm wrong , possibly miss?
double f(a x)
passes x
value passing object of type b
slice off derived virtual functions.
to fix need pass const a&
:
double f(const a& x)
Comments
Post a Comment