[C++-sig] Extending classes

Holger Brandsmeier brandsmeier at gmx.de
Fri Dec 7 14:30:16 CET 2012


Dear Karl,

I have one idea that may help you.

Say you have a class `MyClass` with a method `func` where you want
extra code to be excecuted when that function is called from python. I
would add a function `MyClass_func` which is a function outside a
class that has the same return code as `func` and takes the same
arguments, excepts that it additionally has `MyClass& self` as its
first argument. The type `MyClass&` can also be varied, e.g.
`boost::shared_ptr<MyClass>`. Inside `MyClass_func` you can do
whatever you want, and finally you call `self.func(...)`. In
boost::python you simply export `&MyClass_func` instead of
`&MyClass::func`.

This worked for some of my applications, but might not be suitable in
some inheritance cases. I would need more information on your
application to see if this would be applicable.

-Holger

On Fri, Dec 7, 2012 at 2:22 PM, Charly Bicker <legordian at gmx.net> wrote:
> Dear all,
>
> currently, I am revisiting an problem I had with my project some time ago: due to several external constraints, I need to do some work if certain member functions of my classes are called from python. Initially, I put the additional code in the wrappers which were exposed with boost::python. Unfortunately, I also have cases were objects are instantiated in C++ parts of the code and then handed to python-space, which results in problems when the member functions mentioned above are called. I described the problem already earlier, cf. http://mail.python.org/pipermail//cplusplus-sig/2012-September/016753.html.
>
> Unfortunately, I never found a solution to this problem. Can I somehow execute additional code when member functions are called from python, even if the object was instantiated in C++ (i.e. is not a wrapper class). Is there a way to use a global function for this? I cannot touch the C++ code too much, although a "friend" declaration might be possible.
>
> Thanks in advance for your help!
>
> Best regards,
> Karl Bicker
> _______________________________________________
> Cplusplus-sig mailing list
> Cplusplus-sig at python.org
> http://mail.python.org/mailman/listinfo/cplusplus-sig


More information about the Cplusplus-sig mailing list