[C++-sig] Re : Re : Re : [boost] Re : Using an element of a class A in a constructor of a class B (reflection with boost::python)

Jim Bosch talljimbo at gmail.com
Wed Mar 21 17:32:53 CET 2012


On 03/21/2012 12:09 PM, christophe jean-joseph wrote:
>
>
> Thank you for your answer,
>
> As I said, the solution I am currently using is working fine, I am just using a method explained in the tutorial for a function independant from any class:
>
> http://wiki.python.org/moin/boost.python/ExportingClasses
>
>
> and I extend it to a function of another class.
> You recommend to write things that way:
>
> class_<  A_i, bp::bases<B>  >(...);
>
>
> but, A_i are not derived from B, and as they are already derived from A_Base_j classes (some from a same base class, not all of them), I already declared their base classes.
> Declaring a function B::f(A&  a, ...) in A as:
> .def("f",&B::f)
> keep C++ declaration (I mean, even if B::f is declared in A, it's declared as a method of B, which is correct).
> But what your are proposing seems not correct to me, as long as A isn't a derived class from B.
>

Oh, I understand now.  "B::f" is a static member function that takes an 
"A" as its first argument.  You just want a more elegant way to wrap a 
lot of similar classes.

You may not be able to get it a lot cleaner, but you can cut down some 
of the boilerplate by writing a templated function to wrap an "A" class. 
  You can then just call that repeatedly:

template <typename T>
void wrapA(char const * name) {
     bp::class_<T>(name, ...)
         .def("F", &B::f)
         ;
}

BOOST_PYTHON_MODULE(whatever) {
     wrapA<A_1>("A_1");
     wrapA<A_2>("A_2");
     ...
     wrapA<A_n>("A_n");
}

Of course, you'll have to modify it to do more than that, but hopefully 
that will get you started.

Jim


More information about the Cplusplus-sig mailing list