我有以下课程:
class Student
{
private:
std::string firstName;
std::string lastName;
public:
Student():firstName(""), lastName("")
{
}
Student(const std::string &first, const std::string &last)
:firstName(first), lastName(last)
{
}
Student(const Student &student)
:firstName(student.firstName), lastName(student.lastName)
{
}
Student(Student &&student)
{
firstName=std::move(student.firstName);
lastName=std::move(student.lastName);
}
// ... getters and setters
};
我这样使用它:
std::vector<std::shared_ptr<Student>> students;
std::shared_ptr<Student> stud1 = std::make_shared<Student>("fn1","ln1");
students.push_back(stud1);
Student stud2("fn2","ln2");
students.push_back(std::make_shared<Student>(std::move(stud2)));
根据我的阅读,编译器自动生成了移动构造函数。现在,当我踏入这条线时,students.push_back(std::make_shared<Student>(std::move(stud2)));
我到达了移动构造函数,这没关系。
如果我在进入该行时注释掉移动构造函数,我会到达复制构造函数。我不明白为什么会这样。