[C++-sig] A callback function with arguments causes error

Bruce Who bruce.who.hk at gmail.com
Mon May 14 11:25:51 CEST 2007


Hi, all

Has nobody tried a callback function with arguments? I just want to
implement an event-handling by doing this, but I donnot know how to
make it work.

On 5/10/07, Bruce Who <bruce.who.hk at gmail.com> wrote:
> Hi, all
>
> I have some callback functions in my .cpp files and I want to wrap
> them into python scripts with swig. So I have tried this example
> first: swigwin-1.3.31/Examples/python/callback. This example compiles
> and runs well. But this callback function takes no arguments, so I
> changed it a little:
>
> // example.h
> class Callback {
> public:
>         virtual ~Callback() { std::cout << "Callback::~Callback()" << std:: endl; }
>         // XXX this is the original statement
>         // virtual void run() { std::cout << "Callback::run()" << std::endl; }
>         virtual void run(int* pn) { std::cout << "Callback::run()" << (*pn)
> << std::endl; }
> };
>
>
> class Caller {
> private:
>         Callback *_callback;
> public:
>         Caller(): _callback(0) {}
>         ~Caller() { delCallback(); }
>         void delCallback() { delete _callback; _callback = 0; }
>         void setCallback(Callback *cb) { delCallback(); _callback = cb; }
>         // XXX void call() { if (_callback) _callback->run(); }
>         void call()
>         {
>                 if (_callback)
>                 {
>                     int i = 90;
>                     // XXX _callback->run(i);
>                     _callback->run(&i);
>                 }
>         }
> };
>
>
> As you see, I just add a int* argument to this callback. It compiles
> but it cause python.exe to crash while running. Could anybody tell me
> how to solve this? Any help would be appreciated!


Bruce



More information about the Cplusplus-sig mailing list