Pointer to a class'es member function

I am trying to create a function in a class (it's not a static member) and push it into a vector.

Here is all the necessary code (around 40 lines)
The problem is found at line 29. I can't get that darn little function passed through the parameter!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
namespace GUI
{
	class ToolButton
	{
	private:
		Texture myIconTexture;
		Graphics::Billboard myIconBillboard;
		UserInterface::UI_Box *myBox;
		void (*myClickAction)();

	public:
		ToolButton(void *pClickAction)
		{
			myClickAction = (VOID_FUNCTION)pClickAction;
		}

		void myMouseClick()
		{
			if (windowCursorX > myBox->x && windowCursorY > myBox->y && windowCursorX < myBox->x+myBox->width && windowCursorY < myBox->y+myBox->height)
			{
				myClickAction();
			}
		}

		void Implement(UserInterface::UI_Container &pContainer, Input::ButtonLayer &pButtonLayer, const char *pictureFileName)
		{
			myBox = new UserInterface::UI_Box(20, 20, 2);
			pContainer.AddChildBox(*myBox, UI_left, UI_top);
			pButtonLayer.AddAction(&GUI::ToolButton::myMouseClick);

			Graphics::LoadTexture(pictureFileName, myIconTexture);
			myIconBillboard.Prepare(0.0f, 0.0f);
		}

		void Render()
		{
			myIconBillboard.Render(myIconTexture, myBox->x, myBox->y, 20, 20);
		}
	};
};


Yep, I just wanted to show every one what I've been working on. It's a tool-button class which handles a lot of things, from the billboard that renders the texture as a 2D button, to highly customizable layouts for interface. There's another namespace, UserInterface, which handles the position/layout of interface. But GUI wraps everything else. I don't want to use .NET or any kind of highly abstract framework because that would mean revoking one of the key concepts the project will feature. It's just going to wrap windows, mac, and linux using Direct3d9 and OpenGL. You might think I'm crazy, but that just means you don't REALLY know how to work with these APIs and get them laying on the nitty-gritty.. but allow for high-cross compatability (based on a system of preprocessor conditions), and even build it into an easy to use system for the user of the framework. Also, the way the system requires you to use the framework means that your things could be easily ported down to smaller resolutions, so if I get this working with the hand-held frameworks, XOS for iPod touch and iPhone, and Zune 7, users can rapidly construct games and applications with no fear. It's also for a project called UGE, which will have many tools for creating your own games, by helping the process of developing things from 3D levels to texture maps, shaders, or even little basic elements such as map nodes (for placing objects that aren't static parts of the map) .etc

I'm quite excited because I've already have a lot of the fundamental system working!
Last edited on
What is the prototype for Input::ButtonLayer::AddAction ?
void AddAction(void pFunction());
Have a look at: http://www.parashift.com/c++-faq-lite/pointers-to-members.html. Is there any reason why you can't pass a reference to a ToolButton object to your pButtonLayer class? You could implement the () operator for the ToolButton class to make the syntax nicer also. In fact you could make an abstract base class called Callable or something and then derive any object that you want to pass to your buttonlayer from it:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Callable
{
public:
    void operator()() = 0;
};

class ToolButton : public callable
{
    //toolbutton class def
    void operator()();
}

void ToolButton::operator()()
{
    if (windowCursorX > myBox->x && windowCursorY > myBox->y && windowCursorX < myBox->x+myBox->width && 
    windowCursorY < myBox->y+myBox->height)
    {
    myClickAction();
    }
}



Actually there's another post about this: http://cplusplus.com/forum/beginner/34013/ . This explains how you can do what you're asking (although also warns not to do it!)
Topic archived. No new replies allowed.