#include <iostream>
#include <memory>
using namespace std;
class BaseHolder {
public:
virtual void Set(int) = 0;
virtual int Get() = 0;
virtual BaseHolder *Clone() = 0;
};
template <typename T>
class Holder: public BaseHolder {
public:
void Set(int i) {
if (t == nullptr) {
t = new T();
}
t->Set(i);
}
int Get() {
if (t == nullptr) {
t = new T();
}
return t->Get();
}
virtual ~Holder() {
if (t != nullptr) delete t;
}
BaseHolder *Clone() {
return new Holder<T>();
}
private:
T *t = nullptr;
};
class A {
public:
void Set(int i) {
value = i;
}
int Get() {
return value;
}
private:
int value = 0;
};
int main()
{
BaseHolder *h = new Holder<A>;
BaseHolder *j = h->Clone();
h->Set(5);
j->Set(8);
cout << "h: " << h->Get() << endl;
cout << "j: " << j->Get() << endl;
delete h;
delete j;
return 0;
}