v-table index

Discussion in 'C++' started by Alexander Zezulinsky, May 21, 2011.

  1. Hi, everyone

    I can operate with pointer-to-member functions.
    How can I get virtual member function index from v-table?
    I would like to iterate v-table and to execute virtual method by v-
    table index.

    Is it possible?

    CODE:
    ----------------------------------------------------------------------------------------
    class ICollection
    {
    public:
    virtual void SystemMethod_0() = 0;
    virtual void SystemMethod_1() = 0;
    virtual void SystemMethod_2() = 0;
    virtual void SystemMethod_3() = 0;

    /* pointer-to-member */
    typedef void (ICollection::*fnMethod)();

    /* get pointer-to-member by id */
    virtual ICollection::fnMethod getMethodPtr(size_t nIndex) = 0;

    /* ctor and virtual dtor */
    ICollection() {}
    virtual ~ICollection() {}
    };

    class CTest: public ICollection
    {
    public:
    virtual void SystemMethod_0() {printf("SystemMethod_0");}
    virtual void SystemMethod_1() {printf("SystemMethod_1");}
    virtual void SystemMethod_2() {printf("SystemMethod_2");}
    virtual void SystemMethod_3() {printf("SystemMethod_3");}

    virtual ICollection::fnMethod getMethodPtr(size_t nIndex)
    {
    ICollection::fnMethod fnAction = NULL;
    switch(nIndex)
    {
    case 0: fnAction =
    (ICollection::fnMethod)&CTest::SystemMethod_0;break;
    case 1: fnAction =
    (ICollection::fnMethod)&CTest::SystemMethod_1;break;
    case 2: fnAction =
    (ICollection::fnMethod)&CTest::SystemMethod_2;break;
    case 3: fnAction =
    (ICollection::fnMethod)&CTest::SystemMethod_3;break;
    default: assert(false);break;
    }
    return fnAction;
    }

    /* ctor and dtor */
    CTest() {}
    ~CTest() {}
    };

    /* usage */
    int main(int argc, const char* argv[])
    {
    ICollection* pCollection = new CTest();

    ICollection::fnMethod fnAction = NULL;
    for (size_t nIndex=0; nIndex!=4; ++nIndex)
    {
    /* I would like to get v-table index and execute method by v-table
    index */
    /* But I can operate only with pointer-to-memeber like
    this */
    fnAction = pCollection->getMethodPtr(nIndex);
    (pCollection->*fnAction)();
    }

    delete pCollection;

    return 0;
    }
    ----------------------------------------------------------------------------------------

    Thank you for helping me!
     
    Alexander Zezulinsky, May 21, 2011
    #1
    1. Advertising

  2. Alexander Zezulinsky wrote, On 21.5.2011 19:24:
    > Hi, everyone
    >
    > I can operate with pointer-to-member functions.
    > How can I get virtual member function index from v-table?
    > I would like to iterate v-table and to execute virtual method by v-
    > table index.
    >
    > Is it possible?

    It is not. Instead, gather the member function pointers into your own array
    and index into such array, if you really need to. Alternativelly, rethink
    your design so that you do not need such thing at all.

    >
    > CODE:
    >[...]


    --
    VH
     
    Vaclav Haisman, May 21, 2011
    #2
    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. karthikeyavenkat
    Replies:
    2
    Views:
    597
    Bryce
    Mar 17, 2005
  2. Shawn W_
    Replies:
    5
    Views:
    284
    Aldric Giacomoni
    Sep 16, 2009
  3. ngoc
    Replies:
    5
    Views:
    177
    Tad McClellan
    May 11, 2006
  4. Tomasz Chmielewski

    sorting index-15, index-9, index-110 "the human way"?

    Tomasz Chmielewski, Mar 4, 2008, in forum: Perl Misc
    Replies:
    4
    Views:
    298
    Tomasz Chmielewski
    Mar 4, 2008
  5. Replies:
    3
    Views:
    168
Loading...

Share This Page