Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > v-table index

Reply
Thread Tools

v-table index

 
 
Alexander Zezulinsky
Guest
Posts: n/a
 
      05-21-2011
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;brea k;
case 1: fnAction =
(ICollection::fnMethod)&CTest::SystemMethod_1;brea k;
case 2: fnAction =
(ICollection::fnMethod)&CTest::SystemMethod_2;brea k;
case 3: fnAction =
(ICollection::fnMethod)&CTest::SystemMethod_3;brea k;
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!
 
Reply With Quote
 
 
 
 
Vaclav Haisman
Guest
Posts: n/a
 
      05-21-2011
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index" camelean@shaw.ca ASP .Net 3 02-22-2011 07:06 PM
Program Index: cannot view entire index XP Luke O'Malley Computer Support 2 05-05-2008 03:34 AM
sorting index-15, index-9, index-110 "the human way"? Tomasz Chmielewski Perl Misc 4 03-04-2008 05:01 PM
index.htm or index.html ? Robert Cooze NZ Computing 15 12-13-2005 05:53 PM
problem with index.html .(page is automatically gettin redirected to index.html) karthikeyavenkat Java 2 03-17-2005 10:01 PM



Advertisments