CPP Problem

Hehenberger Robert hehenberger@t-online.de
Sat Jun 16 17:20:00 GMT 2001


Dir Sir's
i got a Problem with the g++ Compiler.
I like to call c++ Class Funktion with a jumptable.
Please see the following example
I hope some one can Help me with this Problem ?
//--------------------------------------------------------------------------
//		Compile : g++ -O2 -ggdb -o cpptab cpptab.C
//--------------------------------------------------------------------------
//#define	DEBUG		1
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <signal.h>
#include <ctype.h>
#include <fcntl.h>
#include <time.h>
//--------------------------------------------------------------------------
//		Defines
//--------------------------------------------------------------------------
#define	byte		unsigned char
#define	word		unsigned short
#define	vint		volatile int
#define	uint		unsigned int
#define	dword		unsigned long
#define	error		(-1)
#define	ERROR		error
//--------------------------------------------------------------------------
//		Hex Dump
//--------------------------------------------------------------------------
void	dump_line(byte *str, int anz)
{
	int	i;

	for(i=0; i< 16; i++){
		if(i < anz)		printf("%02X ", (byte) str[i]);
		else				printf("   ");
		if(i == 7)		printf("- ");
	}
	printf("  >");
	for(i=0; i< 16; i++){
		if(i < anz)		printf("%c", (str[i] >= ' ' && str[i] <= 0x7E) ? str[i] :
'.');
		else				printf(" ");
	}
	printf("<\n");
}
//--------------------------------------------------------------------------
//		Dump Hexline 16 Hex and ascii
//--------------------------------------------------------------------------
void	dump(void *str, int anz)
{
	while(anz >= 16){
		dump_line((byte *) str, 16);
		anz -= 16;
		(char*)str += 16;
	}
	if(anz)	dump_line((byte *) str, anz);
//	printf("\n");
}
//--------------------------------------------------------------------------
//--------------------------------------------------------------------------
#ifdef	DEBUG
void	*operator new(size_t cnt)
{
	void	*p;

	if((p = (void *) malloc(cnt)) == NULL)
		printf("Malloc Error\n");
	printf("new %d at %p\n", cnt, p);
	return(p);
}
void	operator delete(void *ptr)
{
	printf("del %p\n", ptr);
	free(ptr);
}
#endif
//--------------------------------------------------------------------------
//--------------------------------------------------------------------------
//--------------------------------------------------------------------------
class	X	{
public:
	typedef	void	(X::*PTF)();
	typedef	struct	{
		char		*name;
		PTF		fnk;
	} CMDS;
	//------------------------------------------------------------
	X(CMDS *gcmd);
	~X();

	void	run();
	void	ra();
	void	rb();


public:
	int	a;
	CMDS	*fcmd;

};
//--------------------------------------------------------------------------
//--------------------------------------------------------------------------
X::CMDS	gcmd[] = {
	{	"AAAA",	&X::ra	},
	{	"BBBB",	&X::rb	},
};
//--------------------------------------------------------------------------
X::X(CMDS *gcmd)
{
	memset(this, 0, sizeof(X));
	fcmd = gcmd;
}
//--------------------------------------------------------------------------
X::~X()
{
	a = 0;
}
//--------------------------------------------------------------------------
void	X::ra()
{
	printf("RA Called %x\n", a);
}
//--------------------------------------------------------------------------
void	X::rb()
{
	printf("RB Called %x\n", a);
}
//--------------------------------------------------------------------------
void	X::run()
{
	int		i;

	a = 0x99999999;
	printf("X ist runnig %p:%p\n", this, &X::run);
	for(i=0; i< 2; i++){
		printf("Name : %s ", fcmd[i].name);
		printf("Pos  : %p\n", fcmd[i].fnk);
		dump((void*)&fcmd[i].fnk, sizeof(fcmd->fnk));
	}
	printf("\n");
	dump(this, sizeof(X));
	printf("\nADDR of FCMD %p\n", fcmd);
	dump((void*)fcmd, sizeof(gcmd));
	a = 0x111111;
	ra();
	a = 0x222222;
	rb();
	a = 0x333333;
	//------------------------------------------------------------
	//		Here is my problem.
	//		How can i call the funktion X::ra() with jumptable ??
	//		All the following statements got an compile error
	//------------------------------------------------------------
	X::fcmd[0].fnk();
	(fcmd[0].fnk)();
	(*fcmd[0].fnk)();
	(X::fcmd[0].fnk)();
	(X::fcmd[0]->fnk)();
	(*X::fcmd[0]->fnk)();
	(*fcmd.fnk)();
}
//--------------------------------------------------------------------------
//--------------------------------------------------------------------------
//--------------------------------------------------------------------------
int	main()
{
	X	*x = new X(gcmd);
	x->run();
}

-- 

Mit freundlichen Grüßen Robert Hehenberger
Tel.: 08024-9967-0
Fax : 08024-9967-41
cpptab.C
begin:vcard 
n:Hehenberger;Robert
tel;cell:01718021314
tel;fax:080249967-41
tel;home:080249967-0
tel;work:080249967-0
x-mozilla-html:FALSE
url:www.clpgmbh.de
org:Entwicklung;CLP Computer Logistik Planung GmbH
adr:;;Thannerstr.14a;Holzkirchen;Germany;08024;
version:2.1
email;internet:hehenberger@clpgmbh.de
note;quoted-printable:Office Holzkirchen=0D=0ATel.: 08024-9967-0
x-mozilla-cpt:;0
fn:Robert Hehenberger
end:vcard
-------------- next part --------------
A non-text attachment was scrubbed...
Name: cpptab.C
Type: text/x-c++
Size: 4384 bytes
Desc: not available
URL: <http://gcc.gnu.org/pipermail/gcc-bugs/attachments/20010616/4ee79c70/attachment.bin>


More information about the Gcc-bugs mailing list