help : how to : array of method pointers

M

max reason

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
 
P

Peter Koch Larsen

max reason said:
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]
};

//
// 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.
}


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
 
P

Peter Koch Larsen

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

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

Rob Williscroft

max reason wrote in
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 );
}

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

HELP - thanks in advance

Rob.
 
K

Karl Heinz Buchegger

max said:
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?
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;
}
 
D

David Harmon

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?
 
K

Karl Heinz Buchegger

David said:
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.
 
M

max reason

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,159
Messages
2,570,881
Members
47,418
Latest member
NoellaXku

Latest Threads

Top