Answer the question
In order to leave comments, you need to log in
How to make Bind similar to QtSignal?
This code works without problems:
#include <iostream>
#include <functional>
using namespace std;
class Button
{
public:
function<void()> onClick;
void start()
{
onClick();
}
};
class Player
{
public:
Player(Button *btn)
{
btn->onClick = bind(&Player::play, this);
}
void play()
{
cout << "play" << endl;
}
};
int main(void)
{
Button button;
Player player(&button);
button.start();
return 0;
}
#include <iostream>
#include <functional>
using namespace std;
class Button
{
public:
function<void(int)> onClick;
void start()
{
onClick(44);
}
};
class Player
{
public:
Player(Button *btn)
{
btn->onClick = bind(&Player::play, this);
}
void play(int num)
{
cout << "play: " << num << endl;
}
};
int main(void)
{
Button button;
Player player(&button);
button.start();
return 0;
}
Answer the question
In order to leave comments, you need to log in
std::placeholders
btn->onClick = bind(&Player::play, this, std::placeholders::_1);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question