I have two vectors and I want to move an object from one to the other, before I move it I make a pointer to the object. But when I erase the index in first vector then the container within the object is empty if I access it through the pointer. It's difficult to describe, some code will explain it better:
Word.h
class Word
{
public:
Word(string text);
~Word();
string text;
};
Card.h
class Card
{
public:
Card(vector<Word>& words);
~Card();
vector<Word> words;
};
main()
vector<Word> words {
Word("dog"),
Word("cat"),
Word("horse")
};
Card card(words);
vector<Card> deck1;
vector<Card> deck2;
deck1.push_back(move(card));
Card* p = &deck1[0];
deck2.push_back(move(deck1[0]));
cout << p->words.size() << endl; // 3
deck1.erase(deck1.begin());
cout << p->words.size() << endl; // 0 <-- Why is size() 0 here?
p = &deck2[0];
cout << deck2[0].words.size() << endl; // 3 <-- and 3 here
cout << p->words.size() << endl; // <-- and 3 here!
getchar();
The moves have no effect because
Card
has no move constructor or move assignment operator. The vectorsdeck1
anddeck2
contain copies of whatever is pushed into them.But the problem (or at least, one problem) is that you invalidate
p
when you erase fromdeck1
. De-referencingp
is undefined behaviour (UB).