在lambda中,我想捕获一个指针的副本,但是不能,因为它是一个静态变量(无法捕获):
#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".
}
答案 0 :(得分:6)
您可以使用lambda语法ptr
复制[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);
}