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 8358005
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T10:39:52+00:00 2026-06-09T10:39:52+00:00

I have a framework function which expects an object and a member function pointer

  • 0

I have a framework function which expects an object and a member function pointer (callback), like this:

do_some_work(Object* optr, void (Object::*fptr)()); // will call (optr->*fptr)()

How can I pass a lambda expression to it? Want to do somethink like this:

class MyObject : public Object
{
    void mystuff()
    {
        do_some_work(this, [](){ /* this lambda I want to pass */ });
    }
};

The meaning of it all is to not clutter the interface of MyObject class with callbacks.

UPD
I can improve do_some_work in no way because I don’t control framework and because actually it isn’t one function, there’re hundreds of them. Whole framework is based on callbacks of that type. Common usage example without lambdas:

typedef void (Object::*Callback)();
class MyObject : public Object
{
    void mystuff()
    {
        do_some_work(this, (Callback)(MyClass::do_work));
    }
    void do_work()
    {
        // here the work is done
    }
};

SOLUTION Here’s my solution based on Marcelo’s answer:

class CallbackWrapper : public Object
{
    fptr fptr_;
public:
    CallbackWrapper(void (*fptr)()) : fptr_(fptr) { }
    void execute()
    {
        *fptr_();
    }
};

class MyObject : public Object
{
    void mystuff()
    {
        CallbackWrapper* do_work = new CallbackWrapper([]()
        {
           /* this lambda is passed */
        });
        do_some_work(do_work, (Callback)(CallbackWrapper::execute));
    }
};

Since we create the CallbackWrapper we can control it’s lifetime for the cases where the callback is used asynchonously. Thanks to all.

  • 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-06-09T10:39:53+00:00Added an answer on June 9, 2026 at 10:39 am

    This is impossible. The construct (optr->*fptr)() requires that fptr be a pointer-to-member. If do_some_work is under your control, change it to take something that’s compatible with a lambda function, such as std::function<void()> or a parameterised type. If it’s a legacy framework that isn’t under your control, you may be able to wrap it, if it’s a function template, e.g.:

    template <typename Object>
    do_some_work(Object* optr, void (Object::*fptr)());
    

    Then, you can implement a wrapper template:

    template <typename F>
    void do_some_work(F f) {
        struct S {
            F f;
            S(F f) : f(f) { }
            void call() { f(); delete this; }
        };
        S* lamf = new S(f);
        do_some_work(lamf, &S::call);
    }
    
    class MyObject // You probably don't need this class anymore.
    {
        void mystuff()
        {
            do_some_work([](){ /* Do your thing... */ });
        }
    };
    

    Edit: If do_some_work completes asynchronously, you must allocate lamf on the heap. I’ve amended the above code accordingly, just to be on the safe side. Thanks to @David Rodriguez for pointing this out.

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

Sidebar

Related Questions

I have code like this: class ToBeTested { function simpleMethod($param) { if(0 === $param)
I have a function which adds an item to the basket. In this function,
I have Entity Framework entities Events which have an EntityCollection of RSVP. I want
Let's assume I have a framework written in Java and some C++ code which
I have a python framework which has to execute bash scripts as plugins. We
Consider I have built DAL.dll which is a class library containing an Entity Framework
I'm starting out using the Zend Framework and have created a suitable model which
I wrote an application in a JS-based framework called Titanium. This framework doesn't have
So I have two custom complex types like this (oversimplified for this example): public
I have this existing code: Private Function GetTypeFromName(ByVal FullTypeName As String, ByVal AssemblyName As

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.