我从老师那里得到了一个任务,我很难解决它。如何在一个带有一个参数的回调类型中发送一个带有2个参数的函数对象?我可以设法绑定一个带有一个参数的函数,但有两个它不仅改变占位符的数量并重新定义函数<>参数..
#include <functional>
#include <cstdlib>
#include <iostream>
using namespace std;
//definicija klase GLUT
class GLUT {
public:
typedef function<bool (unsigned char)> CallbackType;
GLUT() : kbCallback(NULL) {}
void setKeyboardCB(CallbackType cb) {
kbCallback = cb;
}
void run();
private:
CallbackType kbCallback;
};
void GLUT::run() {
unsigned char c;
while (true) {
if (kbCallback == NULL) {
cout << "Nije definiran keyboard callback" << endl;
break;
}
cin >> c;
if (kbCallback(c) == false)
break;
}
}
class KbHandler
{
public:
KbHandler() {
}
bool Foo(unsigned char c, unsigned char compare) {
if( c == compare )
return false;
cout << "Pritisnut " << c << '\n';
return true;
}
};
int main(int argc, char** argv) {
GLUT glut;
typedef function<bool (unsigned char)> tip;
using std::placeholders::_1;
tip handler;
KbHandler ob;
handler2= std::bind( &KbHandler::foo,ob, _1); //<--he told me here
glut.setKeyboardCB(handler2);
glut.run();
return 0;
}