I'm trying to implement my own std::vector for sake of practice. Current source code:http://pastebin.com/bE4kjzcb
push_back(const T &t)
~T() is exactly how
std::vector handles the problem.
You do however have a couple of problems:
push_back needs to use placement new to copy-construct the value into the vector. You can't just use assignment.
Secondly, you can't call
realloc - if the object have internal pointers, they are going to end up pointing outside them selves. You must call
malloc again, then use placement new to copy-construct the values, then explictly delete all the old values, then call
free to release the old value.
std::vector doesn't call
~T() itself. Instead it calls the allocator which is responsible for ... allocating and deallocating memory. Internally though, that is how general purpose allocators do it.)