回调函数C ++ [英] Callback function C++

查看:110
本文介绍了回调函数C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试建立回调系统时遇到很多问题.我想将一个函数传递给另一个类的函数以接收数据.

I'm having a great deal of problems trying to make a callback system. I want to pass a function to another class's function to receive data.

我希望ExampleClass调用SeperateThread::operate,并且我希望SeperateThread::operate能够调用ExampleClass::updateNumber(int)返回值.我一直在尝试使用各种函数指针等几个小时,但似乎无法使其正常工作.

I want ExampleClass to call SeperateThread::operate, and I want SeperateThread::operate to be able to call ExampleClass::updateNumber(int) to return a value. I've been trying for hours with various function pointers etc but can't seem to get it to work.

SeperateThread是另一个线程,因此它不会阻塞正在运行ExampleClass的主线程,但是SeperateThread完成计算后,我需要将值返回到ExampleClass.

SeperateThread is another thread so it doesn't block the main thread that ExampleClass is running in, but when SeperateThread has done it's calculations, I need to return the value to ExampleClass.

这是否有意义?这是我要做的事情的摘要.在此示例中,我希望SeperateThread::operate调用ExampleClass::updatenumber(15); ...

If that makes sense? Here's a run down of what I'm trying to do. In this example, I want SeperateThread::operate to call ExampleClass::updatenumber(15);...

class ExampleClass
{
    public:
        ExampleClass()
        {   
            int numberToPass = 10;
            // call SeperateThread::operate and pass value and updatenumber function as pointer
            thread.operate(numberToPass, *updatenumber(int number));
        }
        ~ExampleClass();

        void updatenumber(int number)
        {
            // Do some stuff to the number passed to this function
        }

    private:
        SeperateThread thread;
}



class SeperateThread
{
    public:
        SeperateThread();
        ~SeperateThread();

        void operate(int number, &FunctionToCallBack)
        {
            // Do some calculations (result 5 for example purposes)
            int result = numberToPass + 5;

            // Call the callback function and pass result int
            FunctionToCallBack(result);
        }
}

推荐答案

指向类成员函数有一些重要的意义,您必须记住,函数指针只是一个常规指针,而不是它的值指向一个函数,但是在类中有一个特殊的隐藏变量 this ,它使操作变得棘手.

There is something important about pointing to a class member function, you have to keep in mind that a function pointer is just a regular pointer but instead of a value it points to a function, but in a class there is a special hidden variable this which makes it tricky.

这里的主要问题之一是没有指向对象的指针,因为这将意味着您指向特定对象内存在的函数,但它并不只是包含 this的普通函数. 作为参数.

One of the main problems here is that there is no pointer to the object since that would mean that you point to a function that exists within a specific object but it doesn't it just a plain function that contains this as a parameter.

thread.operate(numberToPass,* updatenumber(int number));

thread.operate(numberToPass, *updatenumber(int number));

在这里调用另一个类中的函数,并且总的来说,您永远不会传递这样的指针,它应该仅仅是函数的名称,因为C会识别出您希望将其作为指针传递.通常,解决方法是使函数静态化,以避免this指针出现问题.

Here you call a function that is in another class and overall you never pass a pointer like this, it should be just the function's name since C will recognize that you want to pass it as a pointer. Generally the workaround would be to make the function static to avoid the problem with the this pointer.

一种可能的解决方法是保留该类对象,并以某种方式以恶意方式调用该函数,在该函数中您手动传递原始对象的this(ExampleClass).

One possible workaround would be to hold onto the class object and somehow hackishly call that function where you manually pass the this of the original object ( ExampleClass ).

关于设计,您没有说太多,但是您将源放在同一字段中的事实意味着这些类彼此了解",所以为什么不直接传递类对象并调用该函数呢?像这样:

You didn't say much about your design, but the fact that you put the source into the same field means that these classes "know" each other so why don't you just pass the class object and call the function that way like:

class BaseClass
{
public:

    BaseClass() {}
    ~BaseClass() {}

    virtual void updatenumber(int number)=0;    // pure virutal method, you MUST implement this in the subclasses!
}

class ExampleClass : public BaseClass
{
 public:
    ExampleClass()
    {   
        int numberToPass = 10;
        // call SeperateThread::operate and pass value and updatenumber function as pointer
        thread.operate(numberToPass, this);
    }
    ~ExampleClass();

    // this is now a virtual method
    void updatenumber(int number)
    {
    // Do some stuff to the number passed to this function
    }

 private:
    SeperateThread thread;
}

class SeperateThread
{
 public:
    SeperateThread();
    ~SeperateThread();

    void operate(int number,BaseClass* ObjectToCallBack)
    {
        // Do some calculations (result 5 for example purposes)
        int result = numberToPass + 5;

        // Call the callback function and pass result int
        // Note that here that this points to the BaseClass pointer but it can be a subclass of it effectively hiding it's "unneded members" at this specific point
        ObjectToCallBack->updatenumber(result);
    }
}

如果您想隐藏实现,则可以只使用一个纯虚拟类并将该类型的指针传递给SeperateThread类.

In case you want to hide the implementation you can just use a pure virtual class and pass that type of pointer to the SeperateThread class.

更新了我的示例以使用基类.

Edit : updated my example to use a base class.

这篇关于回调函数C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆