473,698 Members | 2,379 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

help : how to : array of method pointers

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::*dispa tch[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::*pro cess[256])(int*)) = {
&simple::proces s_0x00(int*),
&simple::proces s_0x01(int*),
&simple::proces s_0x02(int*),
// fill rest of the 256 element method-pointer array here
&simple::proces s_0xFD(int*),
&simple::proces s_0xFE(int*),
&simple::proces s_0xFF(int*),
};

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

simple* result = uniform::dispat ch[value](&value);
}
It oughta be simple, right? It is easy in C - why not with C++ ???

HELP - thanks in advance
Jul 22 '05 #1
6 2765

"max reason" <ma*******@NOSP AMmaxreason.com > skrev i en meddelelse
news:10******** *****@corp.supe rnews.com...
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::*dispa tch[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::*pro cess[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::dispat ch[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::*simpl e_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
Jul 22 '05 #2
"Peter Koch Larsen" <pk*****@mailme .dk> skrev i en meddelelse
news:aP******** *************@n ews000.worldonl ine.dk...
simple_functor dispatch[256] =
{
simple::process _0x00,
simple::process _0x01, Missing ampersand. Should be (for all functions):
&simple::proces s_0x01, ....
simple::process _0xFF,
}


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

Jul 22 '05 #3
max reason wrote in news:10******** *****@corp.supe rnews.com:
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::*dispa tch[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::*simpl e::dispatch[3])(int*) =
{
&simple::proces s_0x00,
&simple::proces s_0x01,
&simple::proces s_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.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #4
max reason wrote:

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::*FnctPt r )( 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::dispatc h[] = {
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
kb******@gascad .at
Jul 22 '05 #5
On Wed, 28 Jan 2004 14:39:04 +0100 in comp.lang.c++, Karl Heinz
Buchegger <kb******@gasca d.at> was alleged to have written:
class simple;
typedef simple* ( simple::*FnctPt r )( 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?

Jul 22 '05 #6
David Harmon wrote:

On Wed, 28 Jan 2004 14:39:04 +0100 in comp.lang.c++, Karl Heinz
Buchegger <kb******@gasca d.at> was alleged to have written:
class simple;
typedef simple* ( simple::*FnctPt r )( 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
kb******@gascad .at
Jul 22 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
2755
by: PHPkemon | last post by:
Hi there, A few weeks ago I made a post and got an answer which seemed very logical. Here's part of the post: PHPkemon wrote: > I think I've figured out how to do the main things like storing products in
4
3848
by: Greg Baker | last post by:
I don't know what standard protocol is in this newsgroup. Am I allowed to post code and ask for help? I hope so.. :) Here's my problem: I am trying problem 127 of the valladolid online contests (http://online-judge.uva.es/p/v1/127.html). The program I wrote seems to work fine, but it takes way too much memory to run. I am not that good at programming C++, unfortunately, so I can't seem to find my memory leak. As far as I can tell,...
14
8475
by: dam_fool_2003 | last post by:
Friends, cannot we malloc a array? So, I tried the following code: int main(void) { unsigned int y={1,3,6},i,j; for(i=0;i<3;i++) printf("before =%d\n",y); *y = 7; /* 1*/
7
7406
by: simkn | last post by:
Hello, I'm writing a function that updates an array. That is, given an array, change each element. The trick is this: I can't change any elements until I've processed the entire array. For example, the manner in which I update element 1 depends on several other (randomly numbered) elements in the array. So, I can't change an element until I've figured out how every element changes.
3
2296
by: Jon Skeet | last post by:
I'm trying to speed up a data migration tool I'm writing that uses COM interop. Currently I'm accessing each field within a record individually, which works, but means going across the managed/unmanaged boundary a heck of a lot. The COM object I'm working with has a method which I'm sure *should* help, but I can't get it to work. It's described in the documentation as: GetDataEx method:
8
10716
by: intrepid_dw | last post by:
Hello, all. I've created a C# dll that contains, among other things, two functions dealing with byte arrays. The first is a function that returns a byte array, and the other is intended to receive a byte array as one of its parameters. The project is marked for COM interop, and that all proceeds normally. When I reference the type library in the VB6 project, and write the code to call the function that returns the byte array, it works
23
2535
by: vinod.bhavnani | last post by:
Hello all, I need desperate help Here is the problem: My problem today is with multidimensional arrays. Lets say i have an array A this is a 4 dimensional static array.
9
3223
by: quyvle | last post by:
I can't seem to get this function to work correctly. I'm wondering if anyone could help me out with this. So I'm using the fscanf function to read the input stream and store each string in the appropriate variables. Here's what I'm reading from another file: "# Number of power catergories: 9"
17
7248
by: =?Utf-8?B?U2hhcm9u?= | last post by:
Hi Gurus, I need to transfer a jagged array of byte by reference to unmanaged function, The unmanaged code should changed the values of the array, and when the unmanaged function returns I need to show the array data to the end user. Can I do that? How?
0
8675
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8604
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9029
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8862
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7729
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4619
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3050
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2331
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2002
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.