From what I understand you want to copy an element at specified index (argument index
) and remove it from vector temp
. This function could look simple like this:
void deleteobject(std::vector<figure3d*> &arr, int index, std::vector<figure3d*> &temp)
{
temp.insert(temp.begin(), arr[index]);
arr.erase(arr.begin() + index);
}
In this case, you don't copy the object itself, but only a reference to it - thus you don't need to free the memory where this object is stored.
Also note that in your function you call delete *i;
which frees the memory where the object pointed by i
is stored - and the pointer that stored in arr
becomes invalid (dangling pointer), because it points to memory that was freed already.
And I also suggest you to use vector of objects rather than vector of pointers to objects - although elements get copied, it's usually fast enough and there is no troublesome memory management connected with it. And in case there is a good reason why you use vector of pointers ( When to use pointers in C++ ),
I suggest you to use vector of smart pointers instead: if you have TR1 or C++11 support, use std::shared_ptr
, otherwise use boost::shared_ptr
( Where is shared_ptr? )