Pointer to member

This is a discussion on Pointer to member within the C++ Programming forums, part of the General Programming Boards category; I was looking at some sample code the other day and saw something I didn't really understand. After looking over ...

  1. #1
    Billy
    Guest

    Question Pointer to member

    I was looking at some sample code the other day and saw something I didn't really understand. After looking over the code I found some comments that said pointer to member function.
    When would you use a pointer to a class function?

  2. #2
    Registered User
    Join Date
    Jul 2002
    Posts
    273
    I wouldn't. It's pretty much not needed with polymorphism. I have used static member functions across dll boundaries but they're really the same thing as global anyway.

  3. #3
    Unregistered
    Guest
    data stored in lists is a classic use of pointers to member functions.

  4. #4
    Registered User
    Join Date
    Dec 2001
    Posts
    421
    there are quite a few uses for pointers to functions. i use them in the games that i make for quick decision making

    rather than

    Code:
    enum States { STATE1, STATE2, STATE3, ... };
    
    switch(gameState)
    {
        case STATE1:
            {
                func1();
                break;
            }
        case STATE2:
            {
                func2();
                break;
            }
        case STATE3:
            {
                func3();
                break;
            }
            .
            .
            .
            .
    }
    you can do this:

    Code:
    enum States { STATE1, STATE2, STATE3, ... };
    typedef int (MyClass::*FuncType)(void);
    
    static FuncType funcs[NUM_STATES] =
        {
            &MainWindow::func1,
            &MainWindow::func2,
            &MainWindow::func3,
            .
            .
            .
            .
        };
    
    (this->*funcs[gameState])();
    much quicker, quite funky, and very handy!

    U.
    Quidquid latine dictum sit, altum sonatur.
    Whatever is said in Latin sounds profound.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 05:23 AM
  2. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 11:10 AM
  3. Replies: 0
    Last Post: 03-20-2008, 08:59 AM
  4. Replies: 28
    Last Post: 07-17-2006, 12:35 AM
  5. casting a void* to a pointer to a member function
    By Sebastiani in forum C++ Programming
    Replies: 13
    Last Post: 10-15-2002, 09:57 AM

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