class Test {
int *arr{nullptr};
public:
Test() : arr(new int[5000]{1, 2, 3, 4}) {
cout << "default constructor" << endl;
}
Test(const Test &t) {
cout << "copy constructor" << endl;
if (arr == nullptr)
arr = new int[5000];
memcpy(arr, t.arr, 5000 * sizeof(int));
}
Test(Test &&t) : arr(t.arr) {
cout << "move constructor" << endl;
t.arr = nullptr;
}
~Test() {
cout << "destructor" << endl;
delete[] arr;
}
int *Arr() const { return arr; }
};
int main()
{
Test t(Test{});
// Test t((Test()));
}
In main function, if I changed “Test t(Test{})” to "Test t(std::move(Test{}))" or "Test t(std::forward(Test{}))", it will call once default constructor and once move constructor. But, "Test t(Test{})" itself only call once default constructor, that is so wierd to my understanding. any one can help? very thanks for you!