Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way this lambda can capture a copy of the pointer?

Tags:

c++

lambda

In a lambda I'd like to capture a copy of a pointer, but can't because it's a static variable (can't capture it):

#include <iostream>
#include <string>
#include <functional>

class Window
{public:
    Window(std::string name) : name(name) {}
    std::function<void(int)> eventHandlerFunction;
    std::string name;
};

Window* ptr;

int main()
{
    Window* wnd1 = new Window("wnd1");
    Window* wnd2 = new Window("wnd2");
    ptr = wnd1;

    wnd1->eventHandlerFunction = [=](int n) { std::cout << ptr->name; }; 
// I'd like to capture a copy of the ptr value to wnd1 through the ptr variable

   // BECAUSE THEN
    ptr = wnd2;

    // SOME TIME DOWN THE TRACK THE MOUSE CURSOR ROLLS OVER wnd1, calls eventHandlerFunction
    // and prints "wnd2" instead of "wnd1".
}
like image 897
Zebrafish Avatar asked Nov 25 '25 09:11

Zebrafish


1 Answers

You can copy the current value of ptr with the lambda syntax [local_copy=variable_to_copy_from]:

#include <iostream>
#include <string>
#include <functional>

class Window {
public:
    Window(std::string name) : name(name) {}
    std::function<void(int)> eventHandlerFunction;
    std::string name;
};

Window* ptr;

auto get_ev_handler() {
    return [ptr=ptr](int n) { std::cout << n << ' ' << ptr->name << '\n'; };

    // You can name it something else if you'd like:
    //return [a_copy=ptr](int n) { std::cout << n << ' ' << a_copy->name << '\n'; };
}

int main() {
    Window* wnd1 = new Window("wnd1");
    Window* wnd2 = new Window("wnd2");

    ptr = wnd1;
    wnd1->eventHandlerFunction = get_ev_handler();
    ptr = wnd2;
    wnd2->eventHandlerFunction = get_ev_handler();
    ptr = nullptr;

    // here ptr == nullptr, but these still hold copies of the old values:
    wnd1->eventHandlerFunction(1);
    wnd2->eventHandlerFunction(2);
}
like image 59
Ted Lyngmo Avatar answered Nov 27 '25 22:11

Ted Lyngmo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!