#include #include using namespace std; // my implementation of unique_ptr, without specialization for T[] template struct Box { private: T* ptr{nullptr}; Box(T* n) : ptr{n} {} // private constructor public: Box() {} ~Box() { if (ptr) delete ptr; } T& operator *() { return *ptr; } T* operator ->() { return ptr; } operator bool() { return ptr != nullptr; } // if (box1) .. // move semantics template Box(Box&& other) { ptr = static_cast(other.ptr); other.ptr = nullptr; } template Box& operator =(Box&& other) { if (this == (Box*)&other) return *this; if (ptr) delete ptr; ptr = static_cast(other.ptr); other.ptr = nullptr; return *this; } // delete copy semantics manually Box(const Box& other) = delete; const Box& operator =(const Box& other) = delete; template static Box New(Args&&... args) noexcept { return Box(new T(forward(args)...)); } template friend Box make_box(Args&&... args) noexcept; template friend struct Box; }; template Box make_box(Args&&... args) noexcept { return Box(new T(forward(args)...)); } struct A { //Box n{make_box(1)}; unique_ptr n{make_unique(1)}; virtual int f() = 0; virtual ~A() {} //= default; }; struct B:A { //Box m{make_box(2)}; unique_ptr m{make_unique(2)}; // default destructor overrides virtual destructor. int f() override { return 1; } }; struct B2:B { unique_ptr z{make_unique(3)}; // default destructor overrides virtual destructor. int f() override { return 1; } }; struct C:A { int f() override { return 2; } }; struct D { Box n; }; void willitleak() { //Box b = Box::New(); Box b2 = Box::New(); } int main() { Box a = Box::New(4); Box b = Box::New(5); //a = b; //move(b); a = move(b); Box c = make_box(6); cout << *a << endl; cout << *c << endl; Box n = Box::New(); Box m = Box::New(); //n = m; n = move(m); D d1; D d2 = move(d1); while (1) willitleak(); return 0; }