L
L
LittleBuster2016-02-27 23:46:10
Qt
LittleBuster, 2016-02-27 23:46:10

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;
}

How to make this one work?
#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

1 answer(s)
S
Stanislav Makarov, 2016-02-28
@LittleBuster

std::placeholders

btn->onClick = bind(&Player::play, this, std::placeholders::_1);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question