Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- template<class T>
- T* allocate(size_t n){
- //allocate an array of chars large enough to hold n instances of T
- return (T*)(new char[n*sizeof(T)]);}
- template<class T>
- void deallocate(T* p){
- //delete it as a char array to avoid calling destructors
- delete[] (char*)p;}
- template<class T>
- void construct(T* p,const T& t=T()){
- new ((void*)p) T(t);}
- template<class T>
- void deconstruct(T* p){
- p->~T();}
- class testclass{
- public:
- ~testclass(){
- printf("Deconstructor called");}};
- //example use
- int main(){
- testclass* array=allocate<testclass>(10);
- //increase the virtual array size to 1
- construct<testclass>(array);
- //... do as many construct's as you want
- deconstruct<testclass>(array);
- //... call deconstruct for every construct
- deallocate<testclass>(array);}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement