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:rocess_0x00 (int* value) { // do something }
simple* simple:rocess_0x01 (int* value) { // do something }
simple* simple:rocess_0x02 (int* value) { // do something }
// define rest of the 256 methods here
simple* simple:rocess_0xFE (int* value) { // do something }
simple* simple:rocess_0xFF (int* value) { // do something }
//
// fill the 256-element method-pointer array with method addresses
//
simple* ((uniform::*process[256])(int*)) = {
&simple:rocess_0x00(int*),
&simple:rocess_0x01(int*),
&simple:rocess_0x02(int*),
// fill rest of the 256 element method-pointer array here
&simple:rocess_0xFD(int*),
&simple:rocess_0xFE(int*),
&simple:rocess_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
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:rocess_0x00 (int* value) { // do something }
simple* simple:rocess_0x01 (int* value) { // do something }
simple* simple:rocess_0x02 (int* value) { // do something }
// define rest of the 256 methods here
simple* simple:rocess_0xFE (int* value) { // do something }
simple* simple:rocess_0xFF (int* value) { // do something }
//
// fill the 256-element method-pointer array with method addresses
//
simple* ((uniform::*process[256])(int*)) = {
&simple:rocess_0x00(int*),
&simple:rocess_0x01(int*),
&simple:rocess_0x02(int*),
// fill rest of the 256 element method-pointer array here
&simple:rocess_0xFD(int*),
&simple:rocess_0xFE(int*),
&simple:rocess_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