I have a Foobar class with the sayHello() method that outputs "Hello, hello!". 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;
Output:
Well hello there! Well hello there! vector size: 1
I am confused why this works. Should fooList[0] become null when I take the first step? Why does myFoo2 work?
Here Foobar as follows:
class Foobar { public: Foobar(void) {}; virtual ~Foobar(void) {}; void sayHello() const { cout << "Well hello there!" << endl; }; };
source share