[Q] Pointers to Non-Static class member functions

Discussion in 'C++' started by tekenenSPAM@BUSThotmailE.Rcom, Aug 29, 2003.

  1. Guest

    I know that one cannot cast a non-static member function to a void *.
    (http://users.utu.fi/sisasa/oasis/cppfaq/pointers-to-members.html)

    However, I have a case where I need to call a certain member function
    based on a value passed into another function.

    Since there are a large number of these values and therefore a large
    number of potential functions I need to call, I wanted to avoid using a
    massive switch statement (or equivalently, if/elseif) and to store these
    functions in an efficient data structure so I can extract the member
    function to call based on the value and then call it.

    Now, I come up with a design that I believe will work and would be
    perfectly legal, but I wanted to check with some of the fine folks here
    just to make sure.


    class CAClass;

    typedef long (CAClass::*CAClassFunction)( /*params*/ );

    class CAFunc
    {
    CAFunc( CAClass *obj, CAClassFunction func );
    ~ CAFunc( void ) {}

    long Call( /*params*/ ) { return (mObj->*mFunc)( /*params*/ ); }

    protected:

    CAClass *mObj;
    CAClassFunction mFunc;

    private:
    };


    class CFuncs
    {
    public:

    CFuncs( void );
    virtual ~ CFuncs( void );

    virtual void AddFunc( const long key, void *func );
    virtual void *GetFunc( const long key );

    protected:

    /* storage var here */

    private:
    };


    class CAClass
    {
    public:

    virtual long RandomMemberFunc( /*params*/ );

    protected:

    CFuncs mTheFuncs;
    };


    So, inside of an instance of CAClass, I should be able to do:

    // Add a member func
    mThefuncs.
    AddFunc( uniqueKey,
    new CAFunc( this, &CAFunc::RandomMemberFunc ) );

    // Get and call member func
    CAFunc *func = (CAFunc*)mTheFuncs.GetFunc( uniqueKey );
    if ( func )
    returnValue = func->Call( /*params*/ );


    I've left out various details, but nothing that should be vital.

    So, everything look legal?

    Assuming it is legal, out of curiosity, Would you consider such a thing
    just a "bad idea" and use the massive switch statement anyway?



    --
    , Aug 29, 2003
    #1
    1. Advertising

  2. Yamin Guest

    <> wrote in message
    news:1g0gmt8.y12eeuw2ybtkN%...
    > I know that one cannot cast a non-static member function to a void *.
    > (http://users.utu.fi/sisasa/oasis/cppfaq/pointers-to-members.html)
    >
    > However, I have a case where I need to call a certain member function
    > based on a value passed into another function.
    >
    > Since there are a large number of these values and therefore a large
    > number of potential functions I need to call, I wanted to avoid using a
    > massive switch statement (or equivalently, if/elseif) and to store these
    > functions in an efficient data structure so I can extract the member
    > function to call based on the value and then call it.
    >
    > Now, I come up with a design that I believe will work and would be
    > perfectly legal, but I wanted to check with some of the fine folks here
    > just to make sure.
    >
    >
    > class CAClass;
    >
    > typedef long (CAClass::*CAClassFunction)( /*params*/ );
    >
    > class CAFunc
    > {
    > CAFunc( CAClass *obj, CAClassFunction func );
    > ~ CAFunc( void ) {}
    >
    > long Call( /*params*/ ) { return (mObj->*mFunc)( /*params*/ ); }
    >
    > protected:
    >
    > CAClass *mObj;
    > CAClassFunction mFunc;
    >
    > private:
    > };
    >
    >
    > class CFuncs
    > {
    > public:
    >
    > CFuncs( void );
    > virtual ~ CFuncs( void );
    >
    > virtual void AddFunc( const long key, void *func );
    > virtual void *GetFunc( const long key );
    >
    > protected:
    >
    > /* storage var here */
    >
    > private:
    > };
    >
    >
    > class CAClass
    > {
    > public:
    >
    > virtual long RandomMemberFunc( /*params*/ );
    >
    > protected:
    >
    > CFuncs mTheFuncs;
    > };
    >
    >
    > So, inside of an instance of CAClass, I should be able to do:
    >
    > // Add a member func
    > mThefuncs.
    > AddFunc( uniqueKey,
    > new CAFunc( this, &CAFunc::RandomMemberFunc ) );
    >
    > // Get and call member func
    > CAFunc *func = (CAFunc*)mTheFuncs.GetFunc( uniqueKey );
    > if ( func )
    > returnValue = func->Call( /*params*/ );
    >
    >
    > I've left out various details, but nothing that should be vital.
    >
    > So, everything look legal?
    >
    > Assuming it is legal, out of curiosity, Would you consider such a thing
    > just a "bad idea" and use the massive switch statement anyway?
    >
    >
    >
    > --


    Hey there,

    I have a number of concerns with this. First of all, are the return values
    the same for all the functions? Are the parameter types the same for all
    functions.

    I have no idea how u'd pass different param types to different functions
    without switching/ifing. I assume they are, or ur description above would
    not work. The normal thing then to do is create a function pointer.

    typedef int (*MyFunctionType)(int param1, bool param2);

    Then just add some kind of container class which holds a bunch of these and
    is indexable by a key...maybe a map or a hashtable...or even a vector if the
    keys are 'nice'.

    class FunctionContainer
    {
    ...
    addFunc( MyFunctionType fn);
    MyFunctionType getFunc( int key);
    };

    fill those in and ur done.

    Yamin
    Yamin, Aug 30, 2003
    #2
    1. Advertising

  3. Guest

    Yamin <> wrote:

    > I have a number of concerns with this. First of all, are the return values
    > the same for all the functions? Are the parameter types the same for all
    > functions.


    Yes, which is why I even considered doing something like this to begin
    with.

    Thanks for the confirmation.
    , Aug 30, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Doe
    Replies:
    9
    Views:
    334
    lilburne
    Oct 23, 2003
  2. dolphin
    Replies:
    3
    Views:
    1,318
    Pete Becker
    Dec 5, 2007
  3. Hicham Mouline
    Replies:
    0
    Views:
    413
    Hicham Mouline
    Apr 23, 2009
  4. Hicham Mouline
    Replies:
    1
    Views:
    396
    Michael DOUBEZ
    Apr 24, 2009
  5. paul
    Replies:
    8
    Views:
    693
    Alf P. Steinbach
    Apr 30, 2009
Loading...

Share This Page