clear function [STL]

Hi @ all. I need to do the clear method for my implementation of vector container.
For clear method, i need to know if the element that i'am manipolating is an object or a standard type. If it's an object, i must call her destructor, else i just change the size value of vector, so my problem is to know when the element is an object and when isn't it. Anyone can help me?

sorry for my bad english :(
I'm not sure. Are you implementing your container as a template? If so you could create specializations for the primitive types. (But there are a lot of primitive types.)
How are you implementing the vector? (I don't need the code but a description would be nice) I'm assuming you have a locally declared class inside it to handle each vector item, and perhaps an allocator to handle the memory adjacency?
Can't you just delete them?
If they were newed, but that's why I'm asking how he implemented it. deleting an un-newed object is undefined behavior.
(But then again you could just allocate everything as being a new object. I suppose that makes a lot of sense.)
is a class template. Making the overload of primitive types don't solve the problem. And if the data are a structure? how can i know this?
It's not an overload, it's a specialization. (I agree though, it would be too tedious)
I'd do what Bazzy suggested. Define every single element of the template type as a new object and just delete when you are done. Calling delete on a class object will call the destructor and calling it on a primitive... deletes it. That'll be much easier than trying to differentiate one from the other.
A vector is a class wrapper of a dynamic array, you can call delete[] on the internal array to clear the vector
so i need to do a vector of pointer. when the user call function push_back(for example) i create a new object T(dynamically) and assign the address of the object at the last pointer* in the vector.
right?
If the size is equal to the capacity, push_back reallocates and copies the entire internal array, otherwise it uses the unused part of it to insert new elements
Topic archived. No new replies allowed.