Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you pass a member function pointer?

I am trying to pass a member function within a class to a function that takes a member function class pointer. The problem I am having is that I am not sure how to properly do this within the class using the this pointer. Does anyone have suggestions?

Here is a copy of the class that is passing the member function:

class testMenu : public MenuScreen{ public:  bool draw;  MenuButton<testMenu> x;  testMenu():MenuScreen("testMenu"){     x.SetButton(100,100,TEXT("buttonNormal.png"),TEXT("buttonHover.png"),TEXT("buttonPressed.png"),100,40,&this->test2);      draw = false; }  void test2(){     draw = true; } }; 

The function x.SetButton(...) is contained in another class, where "object" is a template.

void SetButton(int xPos, int yPos, LPCWSTR normalFilePath, LPCWSTR hoverFilePath, LPCWSTR pressedFilePath, int Width, int Height, void (object::*ButtonFunc)()) {      BUTTON::SetButton(xPos, yPos, normalFilePath, hoverFilePath, pressedFilePath, Width, Height);      this->ButtonFunc = &ButtonFunc; } 

If anyone has any advice on how I can properly send this function so that I can use it later.

like image 329
Matt Pascoe Avatar asked Sep 24 '08 22:09

Matt Pascoe


People also ask

How do you get a pointer to member function?

The pointer to member operators . * and ->* are used to bind a pointer to a member of a specific class object. Because the precedence of () (function call operator) is higher than . * and ->* , you must use parentheses to call the function pointed to by ptf .

Can we pass pointer to function in C?

C programming allows passing a pointer to a function. To do so, simply declare the function parameter as a pointer type.

How do you call a member function?

A member function is declared and defined in the class and called using the object of the class. A member function is declared in the class but defined outside the class and is called using the object of the class.


1 Answers

To call a member function by pointer, you need two things: A pointer to the object and a pointer to the function. You need both in MenuButton::SetButton()

template <class object> void MenuButton::SetButton(int xPos, int yPos, LPCWSTR normalFilePath,         LPCWSTR hoverFilePath, LPCWSTR pressedFilePath,         int Width, int Height, object *ButtonObj, void (object::*ButtonFunc)()) {   BUTTON::SetButton(xPos, yPos, normalFilePath, hoverFilePath, pressedFilePath, Width, Height);    this->ButtonObj = ButtonObj;   this->ButtonFunc = ButtonFunc; } 

Then you can invoke the function using both pointers:

((ButtonObj)->*(ButtonFunc))(); 

Don't forget to pass the pointer to your object to MenuButton::SetButton():

testMenu::testMenu()   :MenuScreen("testMenu") {   x.SetButton(100,100,TEXT("buttonNormal.png"), TEXT("buttonHover.png"),         TEXT("buttonPressed.png"), 100, 40, this, test2);   draw = false; } 
like image 87
Commodore Jaeger Avatar answered Sep 25 '22 01:09

Commodore Jaeger