J
Jon Slaughter
I'm having a problem allocating some elements of a vector then deleting
them.
Basicaly I have something like this:
class base
{
private:
std::vector<object> V;
//std::vector<*object> V;
public:
void addObject()
{
object *temp = new object;
V.push_back(*temp);
//V.push_back(new object);
}
void removeObject(index)
{
delete &V[index];
V.erase(V.begin() + index);
//delete V[index];
//V.erase(V.begin() + index);
}
};
the commented line is where I used the pointerized version. In the first
case the object's destructor gets called twice(I suppose because when I call
addObject the local variable goes out of scope(any way to prevent this) and
in the second case it doesn't get called at all ;/
I'd rather not use a vector of pointers though... I thought maybe I could
declare the object *temp as static but this seems to be kinda a hack... I
just need a way to say "don't call the destructor for this local object when
it goes out of scope" or something.
Any ideas?
Jon
them.
Basicaly I have something like this:
class base
{
private:
std::vector<object> V;
//std::vector<*object> V;
public:
void addObject()
{
object *temp = new object;
V.push_back(*temp);
//V.push_back(new object);
}
void removeObject(index)
{
delete &V[index];
V.erase(V.begin() + index);
//delete V[index];
//V.erase(V.begin() + index);
}
};
the commented line is where I used the pointerized version. In the first
case the object's destructor gets called twice(I suppose because when I call
addObject the local variable goes out of scope(any way to prevent this) and
in the second case it doesn't get called at all ;/
I'd rather not use a vector of pointers though... I thought maybe I could
declare the object *temp as static but this seems to be kinda a hack... I
just need a way to say "don't call the destructor for this local object when
it goes out of scope" or something.
Any ideas?
Jon