Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6757889
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:42:33+00:00 2026-05-26T13:42:33+00:00

I am trying to pass a member function to libevent which should be treated

  • 0

I am trying to pass a member function to libevent which should be treated as a callback.

#include <event.h>

class A
{
    public:
        void eventcb(evutil_socket_t fd, short events, void *ctx) { }
};


static void global_eventcb(evutil_socket_t fd, short events, void *ctx) { }

typedef void (A::*mthd)(evutil_socket_t, short, void*);

int main(void)
{
    struct event_base *evbase = event_base_new();

    mthd eventcb = &A::eventcb;
    A *instance = new A;
    (instance->*eventcb)(NULL, 0, NULL);

    struct event *timer1 = evtimer_new(evbase, global_eventcb, NULL);
    struct event *timer2 = evtimer_new(evbase, (instance->*eventcb), NULL);

    return 0;
}   

I can successfully create a method pointer to eventcb in class A and call it on an instance of A (row 20).

Also, passing a global function (as one would do in C) on row 22 also works fine.

However, on row 23, I attempt to pass my method pointer to libevent, and when I compile this I get the following error (using the clang compiler)

example.cpp:23:25: error: no matching function for call to 'event_new'
        struct event *timer2 = evtimer_new(evbase, (instance->*eventcb), NULL);
                               ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from example.cpp:1:
In file included from /usr/local/include/event.h:71:
/usr/local/include/event2/event.h:749:40: note: instantiated from:
#define evtimer_new(b, cb, arg)        event_new((b), -1, 0, (cb), (arg))
                                       ^~~~~~~~~
/usr/local/include/event2/event.h:833:15: note: candidate function not viable: no know conversion from '<bound member function type>' to 'event_callback_fn'
      (aka 'void (*)(int, short, void *)') for 4th argument
struct event *event_new(struct event_base *, evutil_socket_t, short, event_callback_fn, void *);
              ^
1 error generated.

What am I doing wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T13:42:34+00:00Added an answer on May 26, 2026 at 1:42 pm

    Instance method pointers need an instance to be invoked on. Since libevent is a C library, it doesn’t directly provide a mechanism to associate an instance and an instance method, so you’ll have to do it yourself. libevent’s various event creation functions let you pass arbitrary data as a callback argument. The instance pointer can be passed via this argument, either directly or packaged in a class with other arguments if the callback takes additional data. The event callback can be a free function or a static method; which approach to take depends on the class’s responsibility (in the SOLID, single-responsibilty sense).

    An example using a static method and passing no additional data:

    class A {
    public:
        A(struct event_base *);
    
        bool start_timer();
    
        static void invoke_timer_handler(evutil_socket_t fd, short events, void *ctx);
        void handle_timeout(evutil_socket_t fd, short events);
    
    protected:
        struct event_base *evbase;
        struct event *timer;
    };
    
    A::A(struct event_base *event_base) : evbase(event_base), timer(NULL) {}
    
    bool A::start_timer() {
        // not thread safe.
        if (! timer) {
            timer = evtimer_new(evbase, &A::invoke_timer_handler, this);
            return true;
        }
        return false;
    }
    
    void A::invoke_timer_handler(evutil_socket_t fd, short events, void *ctx) {
        (static_cast<A*>(ctx))->handle_timeout(fd, events);
    }
    
    void A::handle_timeout(evutil_socket_t fd, short events) {
        ...
        if (evtimer_del(timer)) {
            // error deleting event
            ...
        } else {
            timer=NULL;
        }
    }
    

    In the example, since A::handle_timeout is only called from within A::invoke_timer_handler, it could be made private or protected.

    The sample has very basic memory management. In general, the code must ensure the instance (and other callback arguments, if the callback argument isn’t simply an A*) exists for the lifetime of the event to prevent access errors. It should also ensure the instance doesn’t leak once the event is no longer needed. If the instance owns the event, memory management is relatively straightforward. Concurrency can also add complication that affect memory management.

    Existing code-level implementations of anonymous functions (e.g. boost::lambda) and the forthcoming lambda expressions from C++11 rely on the function call operator (operator()), which is unsupported in plain C. Thus anonymous functions are unsuitable for use as libevent callbacks or any other C-library callbacks.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi I am trying to pass a callback which is a member function. I
I am trying to pass a member function within a class to a function
In Javascript I'm trying to pass a class member to a jQuery function, but
I am trying to pass a member function to QtConcurrent::run() I have tried to
I'm trying to pass a member function as an argument. Basically I have a
im trying to pass two parameters to a function, i being an int value
I'm trying to pass an instance of a class between two separate classes, like
I'm trying to write a class that has a generic member variable but is
This is the provided function template I'm trying to use: template <class Process, class
I am trying to pass a function pointer (of type QScriptEngine::FunctionSignature (= QScriptValue (QScriptContext

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.