1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
|
// Ce qu'on te fournit
void* custom_malloc(int type);
void custom_free(void* ptr, int type);
int custom_integerType = 1;
int custom_floatType = 2;
int custom_doubleType = 3;
// Ton wrapper
template <T>
struct Wrapper {
Wrapper() { ptr = (T*)custom_malloc(CustomType); }
~Wrapper() { custom_free(ptr, CustomType); }
const T& get() const { return *ptr; }
void set(T& value) { *ptr = value; }
private:
T* ptr;
static int CustomType;
};
int Wrapper<int>::CustomType = custom_integerType;
int Wrapper<float>::CustomType = custom_floatType;
int Wrapper<double>::CustomType = custom_doubleType;
(...)
Wrapper<int> a;
a.set(42);
cout << a.get() << endl; |
Partager