Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #5277 > unrolled thread

v-table index

Started byAlexander Zezulinsky <palgold88@gmail.com>
First post2011-05-21 10:24 -0700
Last post2011-05-21 20:13 +0200
Articles 2 — 2 participants

Back to article view | Back to comp.lang.c++


Contents

  v-table index Alexander Zezulinsky <palgold88@gmail.com> - 2011-05-21 10:24 -0700
    Re: v-table index Vaclav Haisman <v.haisman@sh.cvut.cz> - 2011-05-21 20:13 +0200

#5277 — v-table index

FromAlexander Zezulinsky <palgold88@gmail.com>
Date2011-05-21 10:24 -0700
Subjectv-table index
Message-ID<c40b4b96-12ab-4ca2-8300-94448f3936fb@gu8g2000vbb.googlegroups.com>
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!

[toc] | [next] | [standalone]


#5282

FromVaclav Haisman <v.haisman@sh.cvut.cz>
Date2011-05-21 20:13 +0200
Message-ID<4DD800D8.6000600@sh.cvut.cz>
In reply to#5277
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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.c++


csiph-web