#include <iostream>
#include <memory>
#include <string>

class Foo : public std::enable_shared_from_this<Foo> {
public:
	Foo(const std::string& i_name) : name(i_name) {}
	
	std::function<void()> GetPrinter() {
		std::weak_ptr<Foo> weak_this = shared_from_this();
		
		return [weak_this]() {
			auto that = weak_this.lock();
			if (!that) {
				std::cout << "The object is already dead" << std::endl;
				return;
			}

			std::cout << that->name << std::endl;
		};
	}

	std::string name;
};

int main() {
	std::function<void()> f;
	
	{
		auto foo = std::make_shared<Foo>("OK");
		f = foo->GetPrinter();
	}
	
	auto foo = std::make_shared<Foo>("WRONG");
	
	f();
	
	return 0;
}
