И так, вот код!

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

std::function<std::string()> func;

void do_save() {
std::string text = "Hello World!";
func = [=]() {
std::string new_text = text + "!!";
return new_text;
};
text = "Hello!";
}

int main() {
do_save();

// Тут я ожидаю "Hello!!!"
// Но печатается "Hello World!!!"
std::cout << func() << std::endl;
}


Я понимаю почему выдает «Hello World!!!», а не «Hello!!!», но как сделать что бы все же выдавалось «Hello!!!»?








 






URL записи