array of function pointers in a class

This is a discussion on array of function pointers in a class within the C++ Programming forums, part of the General Programming Boards category; I have a class member for which I'm trying to create a function pointer that will be stored in an ...

  1. #1
    Registered User mlupo's Avatar
    Join Date
    Oct 2001
    Posts
    72

    array of function pointers in a class

    I have a class member for which I'm trying to create a function pointer that will be stored in an array [of function pointers].
    Given this example:
    Code:
    void CMyClass::MyFunction1(int32 param1, int32 param2){
        ...
    }
    
    /*When the MyClass class is initialized, I want to create a pointer to that function.  
    currently I am doing that in the constructor which I believe is OK to do. 
    */
    void (*f[1])(int32, int32) = {MyFunction1}
    At compile time, the compiler complains:
    error: argument of type 'void (CMyClass:(int32, int32)' does not match 'void (*)(int32, int32)'

    I tried a few things to rectify this based on the error message but only made things worse.
    Any suggestions?

    Thanks in advance...
    Mike
    NEVER PET YOUR DOG WHILE IT'S ON FIRE!

  2. #2
    Registered User mlupo's Avatar
    Join Date
    Oct 2001
    Posts
    72
    Through trial and error I think I may have solved this.

    Code:
    void (CMyClass::*f[1])(int32, int32) = {&CMyClass::MyFunction1}
    If this is glaringly wrong, please post a reply.

    Thanks
    NEVER PET YOUR DOG WHILE IT'S ON FIRE!

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,915
    This should be right.
    Remember that class member functions pointers are not the same as normal functions pointers, because they require an instance to work!
    (MyInstance->*Function_ptr)();
    (MyInstance.*Function_ptr)();
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    For sanity's sake, use typedefs!
    Code:
    typedef void (CMyClass::*MyClassI32I32Ptr)(int32, int32);
    MyClassI32I32Ptr f[1] = {&CMyClass::MyFunction1};
    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
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,598
    For sanity's sake, use typedefs!
    Seconded. Very hard to read without a typedef and more confusing than it actually is just b/c the code is ugly.

  6. #6
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,263
    IMHO, unless you're implementing a state machine, you shouldn't be using member function pointers. Hell, I'd prefer a giant switch statement to that sort of mess.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    The one place I've used member function pointers in real code (not counting pointers immediately bound using Boost.Bind) was in a recursive descent parser, where every production function was a member function. There were two productions similar enough that I wanted to make them one function, but the inner part was different. So I made one function that took a pointer to the member function that parses the inner production.
    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. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 07:54 PM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 12:45 PM
  3. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM
  4. dynamic array of base class pointers
    By Corrington_j in forum C++ Programming
    Replies: 1
    Last Post: 11-16-2003, 05:58 AM
  5. Make an array of function pointers
    By VanJay011379 in forum C Programming
    Replies: 2
    Last Post: 02-09-2003, 06:29 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21