You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

43 lines
799 B
C++

#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();
}