Member function callback

#include <iostream>
#include <pthread.h>

class Core;

class Thread
{
public:
    Thread();
    virtual ~Thread();

protected:
    virtual void run() = 0;
private:
    static void* threadWrapper(void* me);
private:
    pthread_t mThread;
};

Thread::Thread()
{
    pthread_attr_t attr;
    pthread_attr_init(&attr);
    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);

    pthread_create(&mThread, &attr, threadWrapper, this);

    pthread_attr_destroy(&attr);
}

void* Thread::threadWrapper(void* me)
{
    static_cast<Thread*>(me)->run();
    return NULL;
}

Thread::~Thread()
{
    void* dummy;
    pthread_join(mThread, &dummy);
}

class Q : public Thread //Event queue that can be timed
{
public:
    Q(Core* pb, void (Core::*method)()):
                m_pb(pb),m_method(method){}
    void show()
    {
        (m_pb->*m_method)();
    }
private:
    virtual void run()
    {
       for(;;)
       {
           sleep(1);std::cout << "running..." << std::endl;
       }
    }

private:
    Core* m_pb;
    void (Core::*m_method)();
};


class Core //The exellent core class which can be named with framework that handles and solves many complicated things
{
public:
    Core()
    {
        m_a = new Q(this, &Core::show);
    }
    void onSomeWorkDone() //Always be called by other
    {
        m_a->show();
    }
private:

    void show()
    {
        std::cout<<"Executing..."<<std::endl;
    }
private:
    Q* m_a;
};

int main()
{
    Core* c = new Core;
    //w->show();//Invalid, the Core::show() is private
    c->onSomeWorkDone();
    while(1)
    {
     sleep(20);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
This error message usually occurs when you try to pass a non-static member function as a callback function to a Qt signal. To solve this issue, you need to ensure that the member function is declared as `static` or that it is a non-member function. If you cannot declare the member function as static, you can use a lambda function or a `std::bind` expression to wrap the member function into a callable object that can be passed as a callback. For example, suppose you have a class `MyClass` with a non-static member function `myFunction`, and you want to connect a signal to this function: ```cpp class MyClass : public QObject { Q_OBJECT public: void myFunction(int value); }; MyClass myObject; connect(mySignal, &MySignal::valueChanged, &myObject, &MyClass::myFunction); // Error! ``` Since `myFunction` is not static, you will get the error message "reference to non-static member function must be called" when you try to compile this code. To solve this, you can wrap `myFunction` into a lambda function: ```cpp class MyClass : public QObject { Q_OBJECT public: void myFunction(int value); }; MyClass myObject; connect(mySignal, &MySignal::valueChanged, &myObject, [](int value){ myObject.myFunction(value); }); // OK! ``` Alternatively, you can use `std::bind`: ```cpp class MyClass : public QObject { Q_OBJECT public: void myFunction(int value); }; MyClass myObject; connect(mySignal, &MySignal::valueChanged, &myObject, std::bind(&MyClass::myFunction, &myObject, std::placeholders::_1)); // OK! ``` Both of these solutions create a callable object that calls `myFunction` on `myObject` with the correct parameter.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值