I'm making a program in which I have several vectors which contain pointers to object & I need to move those pointers from one vector to another.
Now, let's make an example as simple as possible and say I have 2 vectors: atHome & notHome. Both contain pointers to CDs (so we have 2 std::vector<*CD>). Now, I move a CD from my home to a friend's. Thus, the pointer needs to be removed from atHome & added to notHome. The adding is simple enough, I just do a push_back(). However, I can't find a way to remove the pointer from the atHome vector. Both pop_back() & erase() call the destructor of the object they're removing, so I obviously can't use that or I would lose the CD object & the notHome vector would contain an invalid pointer (same goes for clear() if I wanted to remove all the pointers from the vector).
I'm sure there's an easy way to solve this, but I can't come up with one. I know in this example you could just add a location field to the CD class, but in the program I'm making such a thing is not possible and also that would cause an incredible amount of overhead, since each time you'd want to find every CD at home, you'd need to iterate through the complete vector.
And so I continue on my quest to fill this forum with mostly newbie questions.
Bookmarks