Hi,
Now i need onclick function for it, i think it sould be pointer type variable, so i can apply something like this is main.cpp: button.onclick=&mycustomfunction. how do i do that and how do i define the variable?
Here is an example of using pointers to functions:
Code:
#include <iostream>
using namespace std;
class MyButton
{
public:
int x;
int y;
double (*onclick)(int a);
MyButton()
{
x = 0;
y = 0;
onclick = 0;
}
};
double myCustomFunc(int a)
{
cout<<a<<endl;
return a + 2.0;
}
int main()
{
MyButton b;
b.onclick = myCustomFunc;
double result = b.onclick(10);
return 0;
}
A pointer to a function must reflect the type of the function, and the type of a function includes the return type and the parameter types. You should note that myCustomFunc is not a member function of MyButton and therefore it does not have access to MyButton member variables unless they are public.