I am writing a simple code, in which I have a list of objects of class Person. The Person class
class Person
{
private:
std::string name;
std::string surname;
int year;
public:
Person(const std::string& personName,
const std::string& personSurname,
const int& personYear ):
name(personName), surname(personSurname), year(personYear) {}
Person(const Person& p): name(p.name), surname(p.surname), year(p.year) { }
Person(Person &&p) noexcept: name(std::move(p.name)), surname(std::move(p.surname)), year(std::move(p.year)) { }
int getYear()
{
return year;
}
void print()
{
std::cout << name << " " << surname << " " << year << std::endl;
}
};
in which, the move constructor is
Person(Person &&p) noexcept: name(std::move(p.name)), surname(std::move(p.surname)), year(std::move(p.year)) { }
I also have the node structure
struct node
{
Person data;
node *next;
inline node(const std::string& personName,
const std::string& personSurname,
const int& personYear): data(personName, personSurname, personYear), next(nullptr) { }
inline node(const Person& personToInsert): data(personToInsert), next(nullptr) {}
inline node(Person &&personToInsert): data(std::move(personToInsert)), next(nullptr) {}
};
whose move constructor is
inline node(Person &&personToInsert): data(std::move(personToInsert)), next(nullptr) {}
and finally I have the list class
class list
{
private:
node *head;
public:
list();
~list();
void insert(const std::string&, const std::string&, const int& );
void insert(const Person& );
void insert(Person &&);
void print();
};
whose move constructor is
void list::insert(Person &&personToInsert) {
node *new_node = new node(std::move(personToInsert));
if(head == nullptr || (head->data).getYear() >= (new_node->data).getYear())
{
new_node->next = head;
head = new_node;
}
else
{
node *current = head;
while(current->next != nullptr && (current->next->data).getYear() < (new_node->data).getYear())
{
current = current->next;
}
new_node->next = current->next;
current->next = new_node;
}
}
My question is: inside the move constructors, is the use of std::move
correct? Particularly in the first line of code of list::insert