Thread: Table pointer to class functions.

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    450

    Table pointer to class functions.

    I am having problems with a table of pointers to functions within a class.

    I have a user intreface class that accepts messages in the form of a vector of strings. I want one function parsemessage() to index a table of class functions and be the main driver for the class.

    I declare the array as private:
    Code:
    void (*FunctionTable[10])();
    but am having problems initializing the table in my constructor.
    Code:
    FunctionTable[1]=UserInterface::ShowMenu;
    unfortunately this does not work. I tried various other ideas but can't seem to get the right syntax.

    Finally is it possible to use a vector of pointers to functions and if so what is the proper way to do this for a class (within that class)

    I would post the code but would have to reboot into Linux to do it. Hopefully there is enough info for assistance.

  2. #2
    Registered User
    Join Date
    Jul 2003
    Posts
    450
    This must be one of those bad questions. I will post the exact error message and things I tried tomorrow when I boot into linux.

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Is ShowMenu() a static member function?

    One solution is a pointer to a member function such as this:

    typedef void (UserInterface::*pfnShowMenu)();

    Kuphryn

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Non-static member functions are special, in that they have an implicit additional parameter, thus they are not compatible with non-member functions.

    The Boost.Function and Boost.Bind libraries could help resolve your problem.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Registered User
    Join Date
    Jul 2003
    Posts
    450
    Non-static member functions are special, in that they have an implicit additional parameter, thus they are not compatible with non-member functions.
    Help me to understand this.
    The functions I want to put in the table are not static. The problem I am having is matching the signatures. So you are saying the implicit parameter in class methods (non-static) is what is causing this?

    It appears that I will have to use another method which is disappointing. My prof will look down on using a non standard library even if it is popular.

    I can just call the functions directly, but my idea would have been a little more elegant for what I intended.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    http://www.function-pointer.org/
    This is really good at explaining all the intricacies of function pointers

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Several libraries within Boost (e.g. Boost.Bind) are already proposed for the next revision of the C++ standard and will very likely be accepted.
    Be that as it may...

    The main question is, are all the functions you use from the same class? If so, then you just have to match the signature. If not, there is no signature that matches all. Except if you effectively re-write Boost.Function.

    The signature of a member function is
    typedef return-type (class-name::*type-name)(arguments);
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with FIFO QUEUE
    By jackfraust in forum C++ Programming
    Replies: 23
    Last Post: 04-03-2009, 08:17 AM
  2. Replies: 7
    Last Post: 11-17-2008, 01:00 PM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 05:01 PM
  5. Replies: 3
    Last Post: 10-31-2005, 12:05 PM