Chris,

try this...skeletal but you should get the idea...it shows a general callback that returns void and takes a UInt16 and a Char* in this case...

class Foo
{
        typedef void (Foo::*CALLBACKFN)(UInt16,Char*);

        void fn1( UInt16, Char* );
        void fn2( UInt16, Char* );
        void fn3( UInt16, Char* );

        Foo::CALLBACKFN m_fnToUse;
        UInt16 m_uiFnID;
};

then in your code you can declare a static array, for example,

static Foo::CALLBACKFN sg_FnTable[3] = {
        &Foo::fn1,
        &Foo::fn2,
        &Foo::fn3
};

and then you can call the relevant function like so:

void Foo:someFunctionOrOther( UInt16 fnIDToCall, UInt16 arg1, Char* arg2P )
{
((*this).*sg_FnTable[fnIDToCall])( arg1, arg2P );
}



I use C++ callbacks on instance side objects all the time...a mental hangover from my early assembler days and making the universe and its dog a table driven affair to avoid branches blah blah blah. It's also for good for creating convoluted behaviour that not even you can trace!


Sean Charles


On Wednesday, March 5, 2003, at 02:47 PM, Chris Apers wrote:


Hi,

I have i little problem and perhaps you can help.
In the following exemple, i'd like to call
func1, func2 or func3  with _func depending of the configuration
of the app, so i call setfunc(int) to set the _func value.
But i have the following error :

--| illegal implicit convertion from 'void (B::*)()' to 'void (*)()'.

I know my typedef declaration is wrong but i can find how to declare it.

thanks
------------------
class A {
...
};

class B : public A {
public :
    void setfunc(int);
    void funcexec();

...

private:
    typedef void ExecProc(void)
    ExecProc *_func;

    void func1();
    void func2();
    void func3();

};

void B::setfunc(int what) {

    switch (what) {
        case 1:
            _func = func1;
            break;

        case 2:
            _func = func2;
            break;

        case 3:
            _func = func3;
            break;
    }

}

void B::funcexec() {

    _func();
}

--
For information on using the Palm Developer Forums, or to unsubscribe, please see http://www.palmos.com/dev/support/forums/




--
For information on using the Palm Developer Forums, or to unsubscribe, please see 
http://www.palmos.com/dev/support/forums/

Reply via email to