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: .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.

AMDG On 03/10/2012 04:03 PM, christophe jean-joseph wrote:
I am declaring f under both its class B and A_Base as follow: .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.
http://www.boost.org/libs/python/doc/tutorial/doc/html/python/exposing.html#... In Christ, Steven Watanabe

Thank you for your message, but it does not answer my question, I know how to reflect a C++ constructor under Python through boost and have absolutly no problem with that. My question is about reflecting Bctor(A& a, double& x) where Bctor is the constructor of class B and A another class. As I said, I can do that for any other function, except a constructor. I already read many website, including python and boost websites, before asking my question here. The link you provided, and that I already read before, only explain how to reflect a constructor. That would be of great help if anyone could give me some clue. Jean-Joseph Christophe ________________________________ De : Steven Watanabe <watanabesj@gmail.com> À : boost@lists.boost.org Envoyé le : Dimanche 11 mars 2012 2h49 Objet : Re: [boost] Using an element of a class A in a constructor of a class B (reflection with boost::python) AMDG On 03/10/2012 04:03 PM, christophe jean-joseph wrote:
I am declaring f under both its class B and A_Base as follow: .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.
http://www.boost.org/libs/python/doc/tutorial/doc/html/python/exposing.html#... In Christ, Steven Watanabe _______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost

AMDG On 03/10/2012 06:32 PM, christophe jean-joseph wrote:
Thank you for your message, but it does not answer my question, I know how to reflect a C++ constructor under Python through boost and have absolutly no problem with that. My question is about reflecting Bctor(A& a, double& x) where Bctor is the constructor of class B and A another class. As I said, I can do that for any other function, except a constructor. I already read many website, including python and boost websites, before asking my question here. The link you provided, and that I already read before, only explain how to reflect a constructor.
I'm afraid I don't understand. How is what you're trying to do different from reflecting a constructor?
That would be of great help if anyone could give me some clue.
In Christ, Steven Watanabe

On 03/10/2012 09:32 PM, christophe jean-joseph wrote:
Thank you for your message, but it does not answer my question, I know how to reflect a C++ constructor under Python through boost and have absolutly no problem with that. My question is about reflecting Bctor(A& a, double& x) where Bctor is the constructor of class B and A another class. As I said, I can do that for any other function, except a constructor. I already read many website, including python and boost websites, before asking my question here. The link you provided, and that I already read before, only explain how to reflect a constructor. That would be of great help if anyone could give me some clue.
If the "double &" argument is intended to return a value, then I'm afraid you're pretty much stuck, unless you're willing to put the value in some kind of mutable proxy object (e.g. a one-element list). Python floats are immutable, so they simply can't be used as output arguments. And since constructors can't return values, you can't just change the signature. That's a Python limitation, not a Boost.Python one. If you don't care about the value of the "double &" argument after the call, you can just pretend that it's passed by value, i.e.: namespace bp = boost::python; bp::class_<B>("B") .def(bp::init<A&,double>()) ; By the way, you'll probably get more attention on Boost.Python questions on the cplusplus-sig@python.org mailing list. Jim

Thank you very much for the answers. @Steven Watanabe. Sorry, I should have been more clear by showing the solution I found for a non constructor. Let's say I have B::Foo(A& a){} (where foo isn't a ctor of B) I reflect it as follow: class_A <A, boost::noncopyable>("A", no_init) //this line can be modified .def("Foo", &B::Foo); class_B <B, boost::noncopyable>("B", init< >()) //this line can be modified .def("Foo", &B::Foo); Meanning I have to declare Foo in both A and B for boost:;python. My problem is that I can't do the same for a ctor. @Jim Bosch thank you for your advices. As I am new with boost, and with the mailing list, I didn't know where I should ask my question. I will try my luck there. Christophe Jean-Joseph ________________________________ De : Jim Bosch <talljimbo@gmail.com> À : boost@lists.boost.org Cc : christophe jean-joseph <jjchristophe@yahoo.fr> Envoyé le : Dimanche 11 mars 2012 4h37 Objet : Re: [boost] Re : Using an element of a class A in a constructor of a class B (reflection with boost::python) On 03/10/2012 09:32 PM, christophe jean-joseph wrote:
Thank you for your message, but it does not answer my question, I know how to reflect a C++ constructor under Python through boost and have absolutly no problem with that. My question is about reflecting Bctor(A& a, double& x) where Bctor is the constructor of class B and A another class. As I said, I can do that for any other function, except a constructor. I already read many website, including python and boost websites, before asking my question here. The link you provided, and that I already read before, only explain how to reflect a constructor. That would be of great help if anyone could give me some clue.
If the "double &" argument is intended to return a value, then I'm afraid you're pretty much stuck, unless you're willing to put the value in some kind of mutable proxy object (e.g. a one-element list). Python floats are immutable, so they simply can't be used as output arguments. And since constructors can't return values, you can't just change the signature. That's a Python limitation, not a Boost.Python one. If you don't care about the value of the "double &" argument after the call, you can just pretend that it's passed by value, i.e.: namespace bp = boost::python; bp::class_<B>("B") .def(bp::init<A&,double>()) ; By the way, you'll probably get more attention on Boost.Python questions on the cplusplus-sig@python.org mailing list. Jim
participants (3)
-
christophe jean-joseph
-
Jim Bosch
-
Steven Watanabe