function pointers

Sep 16, 2013 at 6:56pm
Its been a while since I did C++, and I'm having some trouble making use of an existing library. The declarations are as follows:

typedef void *Ptr; // General Pointer (32-bit or 64-bit)
Button& func(void (*func)(Ptr user)=NULL, Ptr user=NULL);

in the tutorial provided, the code for usage is this:
void SomeFunction(Ptr) { //do stuff }

btnObj = Gui.GetButton("btnName");
btnObj->func(SomeFunction); //this somehow calls the function pointer when the rectangle position registers a mouse click event

When I try to compile it as is, it gives a C3867 error: SomeFunction call missing argument list; use '&ClassName::SomeFunction' to create pointer to member.
Which I do, and it causes a C2664 error: Library:Button &Library:Button:func(void(__cdecl *)(Ptr)(Ptr)' cannot convert parameter 1 from void(__thiscall ClassName::*)(Ptr) to void(__cdecl *)(Ptr)'

So I am most very confused about what is actually going on here. If anyone can enlighten me, it would be greatly appreciated.
Sep 16, 2013 at 7:06pm
void (*func)(Ptr user) is a pointer to a non-member (or static member) function.

based on the error message, your SomeFunction is a member function (within class ClassName). It cannot be used with this API (you may have to write a non-member function that fishes out a pointer to an object of ClassName type from the void* Ptr and calls your class member function on that object - that's a common pattern with these C-style callbacks)
Last edited on Sep 16, 2013 at 7:08pm
Sep 16, 2013 at 7:09pm
closed account (o1vk4iN6)
A member function and a function aren't the same thing. A member function silently passes an object as a parameter which can be accessed through the keyword "this".

1
2
3
4
5
6
7
8
9
10
11
12

void bar();

class Foo
{
public:
    void bar();
};

void (Foo::* ptr1)()  = &Foo::bar;
void (* ptr2)()       = &bar;
Sep 16, 2013 at 7:59pm
ty for the quick replies. I've worked it out succesfully. :)
Topic archived. No new replies allowed.