I was trying to move an instance to another one, as follows:
#include <iostream>
class student {
public:
student() = default;
student(const student& student) {
std::cout << "copy student" << std::endl;
}
student(student&& student) {
std::cout << "move student" << std::endl;
}
virtual ~student() = default;
};
class undergraduate: public student {
public:
undergraduate(): student() {}
~undergraduate() override = default; // removing this line will make std::move work
};
int main() {
undergraduate student;
undergraduate student1 = std::move(student);
}
Here is the output:
copy student
As you can see, std::move didn't work, student was copied instead of moved, however, if I remove undergraduate's destructor, i.e. the following line:
~undergraduate() override = default; // removing this line will make std::move work
The output will become move student, which means std::move works. Why? Why didn't std::move work when the derived class' destructor is specified?
