Pointer function to member function of pointer to class

Nov 29, 2012 at 4:03pm
Hello all! I have been experimenting with function pointers, and have run into a problem. I was planning on using them as handlers for a button class. The guide I was using (http://www.newty.de/fpt/index.html) explains how to create pointers to member functions, but not pointers to member functions of pointer class (pclass->mfunction). Here is the relevant code:

Typedef for function pointer to pointer class
typedef void (*Game::*pVoidPointer)();

Button class taking function pointer:
1
2
3
4
5
6
7
8
9
Button::Button(POINT a_origin, POINT a_size, UINT a_Bitmap, BOOL a_visible, pVoidPointer funcHandler, HDC hDC)
{
  m_origin   = a_origin;
  m_size     = a_size;
  m_pBitmap  = new Bitmap(hDC, a_Bitmap, _hInstance);
  m_visible  = a_visible;

  cout << "Button Created at " << m_origin.x << ", " << m_origin.y << endl;
}


Assignment of class:
_pWarGame = new Game();

Assignment of button:
1
2
  _pStartGame = new Button(POINT_make(_pGame->GetWidth() / 2 - 60, _pGame->GetHeight() / 2 - 40),
                            POINT_make(120, 80), IDB_WARSTART, TRUE, hDC, &_pWarGame::*game_initialize);


The code erroring is the assignment of the button where it sends the pointer to the member function ( &_pWarGame::*game_initialize);)

If anybody could provide tips as to how to use a function pointer to a member function of a pointer to a class, please let me know!

Also, if any more code needs to be provided to get a more complete picture of the problem, or if anything needs explaining, don't hesitate to ask.
Nov 29, 2012 at 4:08pm
I'm not sure if this is causing your problem but in your constructor implementation, your last two parameters are pVoidPointer, HDC

And when you create a new button the last two parameters you send it are HDC, pVoidPointer.

It looks like they're switched in order.
Last edited on Nov 29, 2012 at 4:08pm
Nov 29, 2012 at 4:22pm
"pointers to member functions of pointer class" is not a concept: raw pointer types do not have member functions. Classes do. A member function of a class to which you hold a pointer is no different from a member function of that class in any other context.

typedef void (*Game::*pVoidPointer)();
This pVoidPointer is a pointer to a data member of class Game, and that data member is a pointer to a non-member function that takes no args and returns void:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>

void fun() { std::cout << "called\n"; }

struct Game
{
    void (*fptr)();
};

typedef void (*Game::*pVoidPointer)();

int main()
{
    pVoidPointer ptr = &Game::fptr; // this is how you can initialize it

    Game g;
    g.fptr = fun;

    (g.*ptr)(); // calls fun()
}

demo: http://ideone.com/weX2Du
Last edited on Nov 29, 2012 at 4:25pm
Nov 29, 2012 at 5:35pm
Thank you both, I had the arguments out of order as well as not understanding what I was trying to do. I will apply this and see how it turns out. Thanks again!
Topic archived. No new replies allowed.