diff --git a/seminar04_templates/07_manager/main.cpp b/seminar04_templates/07_manager/main.cpp new file mode 100644 index 0000000..870865b --- /dev/null +++ b/seminar04_templates/07_manager/main.cpp @@ -0,0 +1,42 @@ +#include +#include + +template +class Manager { +private: + T* mPtr; +public: + Manager() { + mPtr = nullptr; + } + void allocate() { + mPtr = (T*)malloc(sizeof(T)); + } + void construct(const T& t) { + new(mPtr) T{t}; + } + T& get() { + return *mPtr; + } + void destruct() { + mPtr->~T(); + } + void deallocate() { + free(mPtr); + } +}; + +int main() { + Manager a; + a.allocate(); + a.construct("Cats and dogs"); + a.get() += " and elephants"; + std::cout << a.get() << std::endl; + a.destruct(); + + a.construct("Sapere aude"); + std::cout << a.get() << std::endl; + + a.destruct(); + a.deallocate(); +}