struct Person
{
int mId;
std::string mName;
Person(int id, std::string name) : mId(id), mName(std::move(name))
{
}
};
struct Node
{
Person mData;
Node* mLeft;
Node* mRight;
Node(Person data) : mData(std::move(data)), mLeft(nullptr), mRight(nullptr)
{}
};
When writing a constructor for my Person class I intially defined my 'name' parameter to be a const reference but was recommended to change it to a simple value and use std move.
I understand this is because a rvalue string could be used to initialize 'Person', in which case the string would be moved into the variable 'name' as opposed to being copied. It would then be further moved into the member variable 'mName'- via std::move. I am also under the impression that this only works for std::string because it defines a move constructor.
What I don't understand is why I'm recommended to use std::move again in the 'Node' class constructor as I have not defined a move constructor for Person. Furthermore, I have noticed that removing the string member variable, mName, from the Person class stops this IDE reccomendation.
My guess is that this is because of the default move constructor of the Person class.
(In case it is helpful, my IDE is VS2022 and the reccomendation is from the extension Resharper')