[C++-sig] Fw: Problems using wrapper.hpp to override virtual functions
David Abrahams
dave at boost-consulting.com
Wed Jan 23 00:59:38 CET 2008
on Tue Jan 22 2008, Tim Spens <t_spens-AT-yahoo.com> wrote:
> Sorry to forward this to you directly but I've noticed you seem to know the most about boost python
> and I haven't received any responses for a while now.
> All I want to accomplish is have c++ call a python function that is
> defined in a .py script.
There are several examples of that available in the libs/python/test
directory.
> If you have a minute please look at my issue.
Your code doesn't look like it's trying to do anything like what you
say you're trying to accomplish above. The following does that:
// get the python module
boost::python::object module = boost::python::import(modulename)
// get the function from the module
boost::python::object fn = module.attr(functionname)
// call the function
fn(3, 4)
> Thanks,
> Tim
>
> ----- Forwarded Message ----
> From: Tim Spens <t_spens at yahoo.com>
> To: Development of Python/C++ integration <c++-sig at python.org>
> Sent: Friday, January 18, 2008 4:51:09 PM
> Subject: Re: [C++-sig] Problems using wrapper.hpp to override virtual functions
>
>
> I must be missing something still.
> I'm trying to get the python overridden functions to be called when the
> c++ virtual function is invoked.
> Here is what I have so far...
>
<snip>
> // Class with one pure virtual function
> struct A {
> virtual ~A(){}
> virtual char const* f() = 0;
> char const* g() { return "A::g()"; }
> };
>
> struct ACallback : A, wrapper<A> {
> char const* f() {
> return this->get_override("f")();
> }
> };
>
<snip>
> int main() {
> while(1){
> ACallback a;
Never do that. class ACallback is purely for the use of the Boost.Python
interface. You shouldn't be creating them.
--
Dave Abrahams
Boost Consulting
http://boost-consulting.com
More information about the Cplusplus-sig
mailing list