I have a class like this:
class Object {
...
}
and a container which essentially consists of a std::vector<Object>:
class ObjectContainer {
public:
void addObject(Object ?? object);
...
private:
std::vector<Object> vector;
}
Now after I have initialized an Object instance I would like to add the instance to the container and forget about i.e. something like:
Container container;
{
Object object(args...);
// Maybe more object initialization
container.addObject( object );
}
Now - if possible I would like the container, i.e. std::vector<Object> to 'take' the current object instance without any copying - is this possible? And would the implementation of Container::addObject be as simple as:
void Container::addObject( Object && object) {
vector.push_back( object );
}
container.addObject( std::move(object) );, keeping the definition asaddObject(Object && object)Objectis cheap to move otherwise you won't get much benefit from this.