Re: Function lookup tables?

From: John Collyer (johncollyer_at_zoominternet.net)
Date: 10/05/03


Date: Sun, 5 Oct 2003 09:58:13 -0400

Ignore any typos the source is good except howto initialize the function
pointer array inside the constructor

"John Collyer" <johncollyer@zoominternet.net> wrote in message
news:3f802381$1_4@corp.newsgroups.com...
> 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}; // semi colen is there typo
> }
> 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" <merkerk@deadspam.com> wrote in message
> news:blmocb$dpfhf$1@ID-133164.news.uni-berlin.de...
> > "John Collyer" <johncollyer@zoominternet.net> wrote in message
> > news:3f7ee0db$1_4@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! =-----

-----= 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! =-----



Relevant Pages

  • Re: Sorting records using sort()
    ... > We have an array of n*m bytes. ... > proxy objects store a pointer to some master descriptor that stores the ... > need a default constructor, the value type needs to be assignable only ...
    (comp.lang.cpp)
  • Re: Question about arrays in objects
    ... >>I want to create a 2d array as an object member. ... > handled in the constructor, you just can't use a plain old array. ... > that is where using a pointer would come in. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Question about arrays in objects
    ... >then put the array in the constructor and use the pointer? ... There's at least one other way to do this, using a static const int ... handled in the constructor, you just can't use a plain old array. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Arrays!
    ... > innitialize a two dimensional array in my constructor of my class. ... you declare and initialize a local variable named ... There is no syntax in C++ to initialize an array that is a member ...
    (microsoft.public.vc.language)
  • Re: cannot have instance field initializers in structs
    ... implement my own default constructor or to manually initialize field ... it would take to allocate my struct. ... if it takes an hour to initialize this ... > array of references and therefore the allocation is always a known thing. ...
    (microsoft.public.dotnet.languages.csharp)