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".
}