C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

help : how to : array of method pointers

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
max reason
Guest





PostPosted: Wed Jan 28, 2004 11:41 am    Post subject: help : how to : array of method pointers Reply with quote



A method in one of my classes needs to call one of 256 other methods
in the same class based on an unsigned 8-bit value (0x00 to 0xFF).
How is this done? Everything I try generates errors. Something
this basic can't be so difficult (or impossible) - can it? Thanks.

The following roughly shows what I want:


class simple {
public:
simple(); // constructor
~simple(); // destructor
int process(); // public method

private:
simple* process_0x00 (int* value);
simple* process_0x01 (int* value);
simple* process_0x02 (int* value);
// declare rest of the 256 methods here
simple* process_0xFE (int* value);
simple* process_0xFF (int* value);
//
// now, declare 256 element array to hold addresses of 256 methods ( process_0x00() to process_0xFF() )
//
static simple* (simple::*dispatch[0x0100])(int* value); // all methods have identical return & argument types

};

//
// then, in the simple.cpp file
//
// write the 256 methods
//
simple* simple::process_0x00 (int* value) { // do something }
simple* simple::process_0x01 (int* value) { // do something }
simple* simple::process_0x02 (int* value) { // do something }
// define rest of the 256 methods here
simple* simple::process_0xFE (int* value) { // do something }
simple* simple::process_0xFF (int* value) { // do something }

//
// fill the 256-element method-pointer array with method addresses
//
simple* ((uniform::*process[256])(int*)) = {
&simple::process_0x00(int*),
&simple::process_0x01(int*),
&simple::process_0x02(int*),
// fill rest of the 256 element method-pointer array here
&simple::process_0xFD(int*),
&simple::process_0xFE(int*),
&simple::process_0xFF(int*),
};

//
// method that calls one of 256 methods in the method pointer array
//
int process (int value) {

simple* result = uniform::dispatch[value](&value);
}


It oughta be simple, right? It is easy in C - why not with C++ ???

HELP - thanks in advance


Back to top
Peter Koch Larsen
Guest





PostPosted: Wed Jan 28, 2004 12:21 pm    Post subject: Re: help : how to : array of method pointers Reply with quote




"max reason" <maxreason (AT) NOSPAMmaxreason (DOT) com> skrev i en meddelelse
news:101f7omqmr0a663 (AT) corp (DOT) supernews.com...
Quote:
A method in one of my classes needs to call one of 256 other methods
in the same class based on an unsigned 8-bit value (0x00 to 0xFF).
How is this done? Everything I try generates errors. Something
this basic can't be so difficult (or impossible) - can it? Thanks.

The following roughly shows what I want:


class simple {
public:
simple(); // constructor
~simple(); // destructor
int process(); // public method

private:
simple* process_0x00 (int* value);
simple* process_0x01 (int* value);
simple* process_0x02 (int* value);
// declare rest of the 256 methods here
simple* process_0xFE (int* value);
simple* process_0xFF (int* value);
//
// now, declare 256 element array to hold addresses of 256 methods (
process_0x00() to process_0xFF() )
//
static simple* (simple::*dispatch[0x0100])(int* value); // all
methods have identical return & argument types

};

//
// then, in the simple.cpp file
//
// write the 256 methods
//
simple* simple::process_0x00 (int* value) { // do something }
simple* simple::process_0x01 (int* value) { // do something }
simple* simple::process_0x02 (int* value) { // do something }
// define rest of the 256 methods here
simple* simple::process_0xFE (int* value) { // do something }
simple* simple::process_0xFF (int* value) { // do something }

//
// fill the 256-element method-pointer array with method addresses
//
simple* ((uniform::*process[256])(int*)) = {

What is uniform???

[snip]
Quote:
};

//
// method that calls one of 256 methods in the method pointer array
//
int process (int value) {

simple* result = uniform::dispatch[value](&value);

You must provide an object to call with.

Quote:
}


It oughta be simple, right? It is easy in C - why not with C++ ???

HELP - thanks in advance


Well... a typedef might make it simpler, perhaps?


class simple {...};
typedef simple* (simple::*simple_functor)(int *value);

simple_functor dispatch[256] =
{
simple::process_0x00,
simple::process_0x01,
.....
simple::process_0xFF,
}

Now call it like this:

simple s1;
int i;

simple *result = (s1.*dispatch[0x67])(&i);

A little bit complicated, that pointer-to-memberfunction syntax, but perhaps
I have not understood your question?

/Peter



Back to top
Peter Koch Larsen
Guest





PostPosted: Wed Jan 28, 2004 12:26 pm    Post subject: Re: help : how to : array of method pointers Reply with quote



"Peter Koch Larsen" <pklspam (AT) mailme (DOT) dk> skrev i en meddelelse
news:aPNRb.80002$jf4.5147421 (AT) news000 (DOT) worldonline.dk...

Quote:
simple_functor dispatch[256] =
{
simple::process_0x00,
simple::process_0x01,
Missing ampersand. Should be (for all functions):

&simple::process_0x01,
Quote:
....
simple::process_0xFF,
}


The code in my original reply is not tested. One fault is above - but there
may be others.




Back to top
Rob Williscroft
Guest





PostPosted: Wed Jan 28, 2004 12:30 pm    Post subject: Re: help : how to : array of method pointers Reply with quote

max reason wrote in news:101f7omqmr0a663 (AT) corp (DOT) supernews.com:

Quote:
A method in one of my classes needs to call one of 256 other methods
in the same class based on an unsigned 8-bit value (0x00 to 0xFF).
How is this done? Everything I try generates errors. Something
this basic can't be so difficult (or impossible) - can it? Thanks.

The following roughly shows what I want:



[snip]

#include <iostream>

class simple
{
public:
int process( int value );

private:
simple* process_0x00 (int* value);
simple* process_0x01 (int* value);
simple* process_0x02 (int* value);

static simple* (simple::*dispatch[3])(int* value);

};

simple* simple::process_0x00 (int* value)
{
std::cerr << "0: " << *value << 'n';
return this;
}
simple* simple::process_0x01 (int* value)
{
std::cerr << "1: " << *value << 'n';
return this;
}
simple* simple::process_0x02 (int* value)
{
std::cerr << "2: " << *value << 'n';
return this;
}

/* Note the extra "simple"
*/
simple* (simple::*simple::dispatch[3])(int*) =
{
&simple::process_0x00,
&simple::process_0x01,
&simple::process_0x02,
};

int simple::process(int value)
{
/* how to call a member pointer
*/
(this->*dispatch[value])(&value);
return value;
}


int main()
{
simple s;

for ( int i = 0; i < 3; ++i ) s.process( i );
}


Quote:

It oughta be simple, right? It is easy in C - why not with C++ ???

HELP - thanks in advance


Rob.
--
http://www.victim-prime.dsl.pipex.com/

Back to top
Karl Heinz Buchegger
Guest





PostPosted: Wed Jan 28, 2004 1:39 pm    Post subject: Re: help : how to : array of method pointers Reply with quote

max reason wrote:
Quote:

A method in one of my classes needs to call one of 256 other methods
in the same class based on an unsigned 8-bit value (0x00 to 0xFF).
How is this done? Everything I try generates errors.

Which errors?

Quote:

It oughta be simple, right? It is easy in C - why not with C++ ???

It is easy with C++ too. You just need to get the syntax right.

In the future please dont' post roughly the code you have but post the
real code you have and add the error message. This will help us because

* we don't have to wade through lots of code just to find the spot
which causes you troubles.

* we don't fix bugs that are not there in your real code.


This compiles without problems.

class simple;
typedef simple* ( simple::*FnctPtr )( int* value );

class simple {
public:
simple(); // constructor
~simple(); // destructor
int process( int ); // public method

public:
simple* process_0x00 (int* value);
simple* process_0x01 (int* value);
simple* process_0x02 (int* value);
// declare rest of the 256 methods here
simple* process_0xFE (int* value);
simple* process_0xFF (int* value);
//
// now, declare 256 element array to hold addresses of 256 methods ( process_0x00() to
process_0xFF() )
//
static FnctPtr dispatch[0x100]; // all methods have identical return & argument types

};

//
// then, in the simple.cpp file
//
// write the 256 methods
//
simple* simple::process_0x00 (int* value) { /* do something */ return 0; }
simple* simple::process_0x01 (int* value) { /* do something */ return 0; }
simple* simple::process_0x02 (int* value) { /* do something */ return 0; }
// define rest of the 256 methods here
simple* simple::process_0xFE (int* value) { /* do something */ return 0; }
simple* simple::process_0xFF (int* value) { /* do something */ return 0; }

//
// fill the 256-element method-pointer array with method addresses
//
FnctPtr simple::dispatch[] = {
simple::process_0x00,
simple::process_0x01,
simple::process_0x02,
// fill rest of the 256 element method-pointer array here
simple::process_0xFE,
simple::process_0xFF,
};

//
// method that calls one of 256 methods in the method pointer array
//
int simple::process (int value) {

simple* result = (this->*dispatch[value])(&value);

return 0;
}

--
Karl Heinz Buchegger
[email]kbuchegg (AT) gascad (DOT) at[/email]

Back to top
David Harmon
Guest





PostPosted: Wed Jan 28, 2004 2:59 pm    Post subject: Re: help : how to : array of method pointers Reply with quote

On Wed, 28 Jan 2004 14:39:04 +0100 in comp.lang.c++, Karl Heinz
Buchegger <kbuchegg (AT) gascad (DOT) at> was alleged to have written:
Quote:
class simple;
typedef simple* ( simple::*FnctPtr )( int* value );

class simple {

Is there a reason you went to extra trouble to put the typedef outside
the class definition, rather than putting it inside and thus making it
private to the class?


Back to top
Karl Heinz Buchegger
Guest





PostPosted: Wed Jan 28, 2004 3:37 pm    Post subject: Re: help : how to : array of method pointers Reply with quote

David Harmon wrote:
Quote:

On Wed, 28 Jan 2004 14:39:04 +0100 in comp.lang.c++, Karl Heinz
Buchegger <kbuchegg (AT) gascad (DOT) at> was alleged to have written:
class simple;
typedef simple* ( simple::*FnctPtr )( int* value );

class simple {

Is there a reason you went to extra trouble to put the typedef outside
the class definition, rather than putting it inside and thus making it
private to the class?

No. No real reason.


--
Karl Heinz Buchegger
[email]kbuchegg (AT) gascad (DOT) at[/email]

Back to top
max reason
Guest





PostPosted: Thu Jan 29, 2004 12:23 am    Post subject: Re: help : how to : array of method pointers Reply with quote


Thanks for pointing out the errors in my question. The following is
the fixed question - the question I actually want answered!


A method in one of my classes needs to call one of 256 other methods
in the same class based on an unsigned 8-bit value (0x00 to 0xFF).
How is this done? Everything I try generates errors. Something
this basic can't be so difficult (or impossible) - can it? Thanks.

The following roughly shows what I want:


class simple {
public:
simple(); // constructor
~simple(); // destructor
int process(); // public method

private:
simple* process_0x00 (int* value);
simple* process_0x01 (int* value);
simple* process_0x02 (int* value);
// declare rest of the 256 methods here
simple* process_0xFE (int* value);
simple* process_0xFF (int* value);
//
// now, declare 256 element array to hold addresses of 256 methods ( process_0x00() to process_0xFF() )
//
static simple* (simple::*dispatch[0x0100])(int* value); // all methods have identical return & argument types

};

//
// then, in the simple.cpp file
//
// write the 256 methods
//
simple* simple::process_0x00 (int* value) { // do something }
simple* simple::process_0x01 (int* value) { // do something }
simple* simple::process_0x02 (int* value) { // do something }
// define rest of the 256 methods here
simple* simple::process_0xFE (int* value) { // do something }
simple* simple::process_0xFF (int* value) { // do something }

//
// fill the 256-element method-pointer array with method addresses
//
simple* ((simple::*process[256])(int*)) = {
&simple::process_0x00(int*),
&simple::process_0x01(int*),
&simple::process_0x02(int*),
// fill rest of the 256 element method-pointer array here
&simple::process_0xFD(int*),
&simple::process_0xFE(int*),
&simple::process_0xFF(int*),
};

//
// method that calls one of 256 methods in the method pointer array
//
int process (int value) {

simple* result = simple::dispatch[value](&value);
}


It oughta be simple, right? It is easy in C - why not with C++ ???

HELP - thanks in advance


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.