Thicc Theo
Thicc Theo

Reputation: 65

Move an object containing a unique_ptr to vector

Just wondering if there is a way to move an object holding a unique_ptr into a vector of those objects? Example:

class A
{
public:
   std::unique_ptr<someData> ptr;
};

std::vector<A> objects;
A myObject;

//move myObject to objects???

Now, is there a way I could move myObject into objects and avoiding unique_ptr errors?

Upvotes: 1

Views: 458

Answers (1)

lorro
lorro

Reputation: 10880

You'll need to do std::move:

std::vector<A> objects;
A myObject;

objects.push_back(std::move(myObject));

Upvotes: 1

Related Questions