The c code for function lookup table works great, but?
I have the lookup table in a class as a member of that class.
I want to initialize the function lookup table array to my member
function calls of the class inside of the constructor for the class.
What is the correct code to initialize the function pointer array
in the constructor? I have:
inside the class header file
typedef void(*func_ptr)(); // declare pointer to function
class myclass
{
public:
myclass();
virtual ~myclass();
func_ptr Functions[5]; // declare the function pointer array
char F0(size_t address); // member functions
char F1(size_t address);
char F2(size_t address);
char F3(size_t address);
char F4(size_t address);
};
Inside the class source cpp file
myclass:myclass()
{
Functions[] = { F0,F1,F2,F3,F4}
}
myclass::~myclass()
{
}
char myclass::F0(size_t address)
{
}
char myclass::F1(size_t address)
{
}
char myclass::F2(size_t address)
{
}
char myclass::F3(size_t address)
{
}
char myclass::F4(size_t address)
{
}
The actual function pointer lookup table array is 256 functions or pointers
to functions, so the initialization list inside the constructor is quite
long and uses more then one line. Everything works except I get errors with
the array initialization list in the constructor; errors dealing with the
initialation of the array.
syntax error : ']'
error C2143: syntax error : missing ';' before '{'
error C2143: syntax error : missing ';' before '}'
error C2059: syntax error : ']'
syntax error : missing ';' before '{'
error C2143: syntax error : missing ';' before '}'
Any help?
John Collyer
"Peter van Merkerk" <me*****@deadspam.com> wrote in message
news:bl************@ID-133164.news.uni-berlin.de...
"John Collyer" <jo*********@zoominternet.net> wrote in message
news:3f**********@corp.newsgroups.com... Hi,
In assembly language you can use a lookup table to call functions.
1. Lookup function address in table
2. Call the function
Like:
CALL FUNCTION[INDEX]
FUNCTION DD FUNC1, FUNC2, FUNC3
FUNC1:
FUNC2:
FUNC3:
RET
How can I do this in C++ I would like it to be reasonably fast also?
The way I am doing it now is with a big switch statement. The switch
uses the index number for lookup. This seems like a bad way to do
what I'd would like. The preferred way would be with the lookup tables.
If anyone could help I would appreciate it.
Try this:
#include <iostream>
using std::cout;
typedef void(*func_ptr)();
void func1()
{
cout << "func1()\n";
}
void func2()
{
cout << "func2()\n";
}
void func3()
{
cout << "func3()\n";
}
int main()
{
func_ptr func[3] = {func1, func2, func3};
for(int i = 0; i < 3; ++i)
{
func[i]();
}
return 0;
}
Depending on the optimization capabilities of your compiler the resulting
code may be as fast as the assembler equivalent.
Note that in C++ polymorphism might be a more appropriate solution than
using function pointers or switches.
--
Peter van Merkerk
peter.van.merkerk(at)dse.nl
-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----