I have a Foobar
class with a sayHello()
method that outputs "Well hello there!". If I write the following code
vector<unique_ptr<Foobar>> fooList;
fooList.emplace_back(new Foobar());
unique_ptr<Foobar> myFoo = move(fooList[0]);
unique_ptr<Foobar> myFoo2 = move(fooList[0]);
myFoo->sayHello();
myFoo2->sayHello();
cout << "vector size: " << fooList.size() << endl;
The output is:
Well hello there!
Well hello there!
vector size: 1
I'm confused why this works. Shouldn't fooList[0]
become null when I do the first move? Why does myFoo2
work?
Here's what Foobar
looks like:
class Foobar
{
public:
Foobar(void) {};
virtual ~Foobar(void) {};
void sayHello() const {
cout << "Well hello there!" << endl;
};
};
Shouldn't fooList[0] become null when I do the first move?
Yes.
Why does myFoo2 work?
It doesn't; it causes undefined behaviour. Your compiler happens to produce code that doesn't crash if you use a null pointer to call a non-virtual function that doesn't dereference this
.
If you change the function as follows, it will be clearer what's happening:
void sayHello() const {
cout << "Well hello there! My address is " << this << endl;
}
Well hello there! My address is 0x1790010
Well hello there! My address is 0
vector size: 1
The answer is: No, move operations doesn't remove elements from containers.
Another comment: the use of the emplace_back function is likely to be inadequate.
try:
vector<unique_ptr<Foobar>> fooList;
fooList.emplace_back( new Foobar );
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With