简体   繁体   English

移动 Person 对象列表的构造函数

[英]Move constructor for a list of Person objects

I am writing a simple code, in which I have a list of objects of class Person.我正在编写一个简单的代码,其中我有一个 class 人的对象列表。 The Person class人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

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?我的问题是:在移动构造函数中, std::move的使用是否正确? Particularly in the first line of code of list::insert特别是在list::insert的第一行代码中

inside the move constructors, is the use of std::move correct?在移动构造函数中, std::move的使用是否正确?

Yes.是的。

Particularly in the first line of code of list::insert特别是在list::insert的第一行代码中

This is correct, too.这也是正确的。

Note, however, that there are two minor things I would like to point out.但是请注意,我想指出两件小事。 First, there is no need to manually define the move constructor for Person .首先,不需要手动为Person定义移动构造函数。 If your class doesn't do manual resource handling (memory, IO handles etc.), just rely on the special member functions that the compiler generates for you.如果您的 class 不进行手动资源处理(内存、IO 句柄等),则只需依赖编译器为您生成的特殊成员函数。 In this case, I would just remove Person 's move and copy constructor.在这种情况下,我将删除Person的移动和复制构造函数。

Second,第二,

inline node(Person &&personToInsert)

is not a move constructor.不是移动构造函数。 It's an ordinary constructor that happens to take an rvalue-reference parameter.它是一个普通的构造函数,恰好带有一个右值引用参数。 Move constructors are for constructing an object from an rvalue reference of their own type.移动构造函数用于从其自身类型的右值引用构造 object。

Third,第三,

void list::insert(Person &&personToInsert)

is not a constructor at all - it's an ordinary member function that happens to accept an rvalue parameter.根本不是构造函数——它是一个普通成员 function 碰巧接受一个右值参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM