seminar 04 07_manager

master
nihonium 2 years ago
parent feef44db4d
commit edf2094701
No known key found for this signature in database
GPG Key ID: 0251623741027CFC

@ -0,0 +1,42 @@
#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();
}