Re : [boost] Re : Using an element of a class A in a constructor of a class B (reflection with boost::python)
Hello The situation is as follow. I have a C++ code that I haven't written and that I barely can modified. I am supposed to reflect this code in Python with boost. In the code, I have something looking like this: Class A_Base { A_Base(){}; ~A_Base(){}; Whatever virtual and pure virtual functions; } Class A_Derived{ A_Derived(Type1 arg1,...){whatever instructions;} ~A_Derived(){}; Whatever functions; } Class B { B(A_Base& aBase, double& x){}; ~B(){}; Whatever functions; } In the C++ main, at some point aDerived A_Derived is set, and then B(aDerived, x). I need to reflect that under python. Until now, I have been able, with a simple example, to reflect a function f, which is not a ctor, from a class B using A_Base& as argument type, but I can't figure out how to deal with it for a constructor. Based on: http://wiki.python.org/moin/boost.python/ExportingClasses I am declaring f under both its class B and A_Base as follow: class_A_Base <A, boost::noncopyable>("A_Base", no_init) //this line can be modified .def("f", &B::f); class_B <B, boost::noncopyable>("B", init< >()) //this line can be modified .def("f", &B::f); But when I try this for a constructor as f, it refuse to compile. Anyone got a clue? Thank you very much in advance for any further help.
On 03/15/2012 09:22 AM, christophe jean-joseph wrote:
Hello
The situation is as follow. I have a C++ code that I haven't written and that I barely can modified. I am supposed to reflect this code in Python with boost. In the code, I have something looking like this:
Class A_Base { A_Base(){}; ~A_Base(){}; Whatever virtual and pure virtual functions; }
Class A_Derived{ A_Derived(Type1 arg1,...){whatever instructions;} ~A_Derived(){}; Whatever functions; }
Class B { B(A_Base& aBase, double& x){}; ~B(){}; Whatever functions; }
In the C++ main, at some point aDerived A_Derived is set, and then B(aDerived, x). I need to reflect that under python. Until now, I have been able, with a simple example, to reflect a function f, which is not a ctor, from a class B using A_Base& as argument type, but I can't figure out how to deal with it for a constructor. Based on:
http://wiki.python.org/moin/boost.python/ExportingClasses
I am declaring f under both its class B and A_Base as follow:
class_A_Base <A, boost::noncopyable>("A_Base", no_init) //this line can be modified .def("f", &B::f); class_B <B, boost::noncopyable>("B", init< >()) //this line can be modified .def("f", &B::f);
But when I try this for a constructor as f, it refuse to compile.
You don't wrap constructors the same way as functions (I don't think you can even take their address in C++). Instead, you use: .def(init<T1,T2,...>()) (where T1,T2,... are the argument types of the constructor), or do the same thing in the init argument to class_ itself (as I've done below). I think what you want is something like this (assuming A_Derived does actually inherit from A_Base, even though it doesn't in your example code): class_<A_Base,noncopyable>("A_Base", no_init); class_<A_Derived,bases<A_Base>,noncopyable>("A_Derived", no_init); class_<B,noncopyable>("B", init<A,double>()); Note that the float you pass in as "x" to B's constructor will not be modified in Python; that's unavoidable in this case. If that doesn't work, please post exactly what you're trying to compile; the example you posted has a few things that obviously won't compile, so it's hard to know exactly what the problem is. Good luck! Jim
participants (2)
-
christophe jean-joseph -
Jim Bosch