I have an object something like the following and I’m trying to implement a move constructor for so you can have an insert for std::vector<Mesh>.
struct Mesh
{
std::vector<Vector3> vPoint;
bool Valid;
Mesh(Mesh&& other)
{
vPoint = std::move(other.vPoint);
Valid = std::move(other.Valid);
}
};
Is this the correct way?
And if so what is the value of other.Valid after std::move operates on it?
Edit:
Also if I have an instance of this object do I need to use std::move in the following scenario?
std::vector<Mesh> DoSomething()
{
Mesh mesh; //Imagine vPoint is filled here to
std::vector<Mesh> meshes;
meshes.push_back(std::move(mesh)); // Here is my question, std::move? or just pass mesh here?
return meshes;
}
You should write your move constructor as follows:
The disadvantage of assignment within the constructor body as opposed to using the constructor initializer list is that in the former case the member objects of the
Meshobject that you’re moving to are default constructed and then assigned to within the body. In the latter case they’re constructed directly from the result of thestd::movecall.You shouldn’t read from an object, be it an integral type, or a more complex object, after moving it. Such objects exist in an unspecified state.