Reputation: 13936
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".
}
Upvotes: 3
Views: 167
Reputation: 117318
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);
}
Upvotes: 6