Source File: ex55b.cpp
#include <fmt/core.h>
#include <chrono>
#include <thread>
#include <string>
#include <functional>
#include <memory>
using std::shared_ptr, std::make_shared, std::string;
struct Person {
std::string name;
int age;
std::function<void()> talk = nullptr;
};
shared_ptr<Person> Person_new(const string& name, int age) {
auto self = make_shared<Person>(name, age);
self->talk = [self]() {
fmt::println("I am {} and I am {} years old.",
self->name, self->age);
};
return self;
}
int main(int argc, char* argv[]) {
auto zed = Person_new("Zed", 51);
auto mary = Person_new("Mary", 28);
zed->talk();
mary->talk();
return 0;
}