Home > Backend Development > C++ > How to Implement Generic C Callbacks Using Class Members and `std::function`?

How to Implement Generic C Callbacks Using Class Members and `std::function`?

Patricia Arquette
Release: 2024-12-07 18:56:18
Original
488 people have browsed it

How to Implement Generic C   Callbacks Using Class Members and `std::function`?

C Callback Using Class Member with Generic Implementation

The original question aimed to create a generic event-handling mechanism that would work consistently across different classes. Instead of relying on static methods and passing around class instance pointers, a more modern C approach can be employed using std::function and std::bind.

Event Handler with std::function

The event handler class now accepts a std::function object as an argument. A function object represents a callable entity that can be passed around like a regular function pointer. The event handler method addHandler takes a std::function as an argument, where the passed function has no return value and takes an integer as an argument.

class EventHandler
{
public:
    void addHandler(std::function<void(int)> callback)
    {
        cout << "Handler added..." << endl;
        // Let's pretend an event just occured
        callback(1);
    }
};
Copy after login

Binding Specific Functions

To bind a specific class method to the event handler, std::bind is used. std::bind specifies the this pointer and the function to be called when the event is triggered.

class MyClass
{
public:
    MyClass();

    // Note: No longer marked `static`, and only takes the actual argument
    void Callback(int x);
private:
    int private_x;
};

MyClass::MyClass()
{
    using namespace std::placeholders; // for `_1`

    private_x = 5;
    handler->addHandler(std::bind(&MyClass::Callback, this, _1));
}

void MyClass::Callback(int x)
{
    // No longer needs an explicit `instance` argument,
    // as `this` is set up properly
    cout << x + private_x << endl;
}
Copy after login

Free-Standing Functions and Lambda Functions

If the callback is a free-standing function without a class context, std::bind is not required.

void freeStandingCallback(int x)
{
    // ...
}

int main()
{
    // ...
    handler->addHandler(freeStandingCallback);
}
Copy after login

For anonymous callbacks, lambda functions can be used with the event handler.

handler->addHandler([](int x) { std::cout << "x is " << x << '\n'; });
Copy after login

In this way, using std::function and std::bind provides a flexible and generic solution for callbacks that can be applied to different classes and functions.

The above is the detailed content of How to Implement Generic C Callbacks Using Class Members and `std::function`?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template