Setting Two Functions Equal
I am trying to make a C++ Button class in which the user inputs a function to execute when the button is pressed. So far, this is my code:
class Button {
public:
char* text;
void* buttonclick();
Button (char* constext, void* consbuttonclick()) {
text = constext;
//What do I do here?
}
};
I am trying to make the constructor pass on the value of consbuttonclick() to buttonclick(). I believe there is a way to do this using function pointers, but I have been having trouble with them, so I would greatly appreciate some 开发者_如何学JAVAhelp on how to proceed.
What you've declared is a function returning a void *. You want a pointer to function returning void. So to start with you need to change the prototype: In place of void * foo()
you want void (*foo)()
. (You'll have to sort out exactly what you want, but that's the idea.)
Once you have it, you call it simply by using the function invocation operator ()
, so you'l get something like
class Button {
public:
char* text;
void* buttonclick();
Button (char* constext, void(* consbuttonclick())) {
text = constext;
consbuttonclick();
}
};
Better yet, though, is to create what's called a functor class, like:
class ClickFunctor {
public:
void doIt(){
// code
}
}
then pass a ClickFunctor object.
class Button {
public:
char* text;
Button (char* constext, ClickFunctor cf) {
text = constext;
cf.doIt();
}
};
Now, go read the C++ FAQ book on these things. (Update: Marshall calls these things "functionoids" rather than "functors", just to warn you.)
Sorry to burst your bubble, but there's no sane way of doing that. Function names only exist for easy writing. As soon as the compiler gets hold of them, the names become garbled and then turned into function addresses only. The names (at least in C++) are meaningless at runtime.
Your best bet is either a long chain of if / else statements OR possibly a dictionary that maps a string to a function pointer. But you'd have to set up that dictionary manually.
Look, if you are trying to make a button in a c++ ms-dos window, it can't be done unless you have a library that I don't about. But I found a way around that (which is long but gets the job done). What I did was I found the top left position of a button drawn out with simple cout<<"whatevergoeshere";, and the bottom right. Then, by creating a function that returns true when the left button is pressed and if the click is within the arguments provided for the top left corner and bottom right corner, control will move on. It is a pretty complicated process, but I can give you the source code I used in another answer.
(If you wan't me to explain any of this code, I will. I didn't know how to do this either until recently. One thing that is important though is keeping the window in the same place everytime. In the environment I'm using (code::blocks), the window stays in the same place, but moving it can mess up the button. Also remember to continuosly use the system("cls"); to clear the screen so stuff doesn't change position. (system"cls" is located in the cstdio or cstdlib libraries.)
Here is the code I used:
#include <iostream>
#include <windows.h>
#include <cstdlib>
#include <cstdio>
#include <stdio.h>
#ifndef MOUSE_HWHEELED
#define MOUSE_HWHEELED 0x0008
#endif
using namespace std;
int main()
{
POINT point;
HANDLE csbiHandle;
CONSOLE_SCREEN_BUFFER_INFO csbi;
int counter = 0;
DWORD cNumRead, i,fdwMode, fdwSaveOldMode;
INPUT_RECORD irInputBuffer[128];
HANDLE stdHandle;
stdHandle = GetStdHandle(STD_INPUT_HANDLE);
MOUSE_EVENT_RECORD mer;
cout << "|-------------|" << endl
<< "| A |" << endl
<< "|-------------|" << endl;
buttonpress:
ReadConsoleInput(stdHandle, irInputBuffer,128, &cNumRead);
GetCursorPos(&point);
for(i=0; i<cNumRead; i++)
{
switch(irInputBuffer[i].EventType)
{
case MOUSE_EVENT:
{
mer = irInputBuffer[i].Event.MouseEvent;
if(mer.dwButtonState == FROM_LEFT_1ST_BUTTON_PRESSED)
{
cout << "left button press" << endl;
cout << point.x << " " << point.y << endl;
if(point.x>=16&&point.x<=182&&point.y>=30&&point.y<=63){cout << "You clicked A!!" << endl;}
}
else
{
goto buttonpress;
}
break;
}
default:{
printf("unknown\n");
break;}
}
}
goto buttonpress;
return 0;
}
精彩评论