This repository has been archived on 2023-05-13. You can view files and clone it, but cannot push or open issues or pull requests.
mipt_cpp/term1/seminar04_templates/07_manager/main.cpp

43 lines
799 B
C++
Raw Normal View History

2023-02-25 19:34:24 +03:00
#include <iostream>
#include <string>
template <typename T>
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<std::string> 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();
}