Copy hoặc move constructor do người dùng viết cho derived class nên khởi tạo base subobject thế nào?
Xem đáp án và giải thích
Derived copy hoặc move constructor do compiler sinh sẽ áp dụng operation tương ứng cho từng base và member. Khi constructor của derived do người dùng viết, nó phải khởi tạo base rõ ràng bằng Base(other) để copy hoặc Base(std::move(other)) để move; bỏ mem-initializer đó sẽ thử default-initialize base thay vì copy hoặc move. Member cũng phải được xử lý theo ownership semantics, còn thứ tự khởi tạo thật vẫn là virtual base, direct base rồi member theo thứ tự declaration bất kể thứ tự chữ trong mem-initializer.
Derived copy hoặc move constructor do compiler sinh sẽ áp dụng operation tương ứng cho từng base và member. Khi constructor của derived do người dùng viết, nó phải khởi tạo base rõ ràng bằng Base(other) để copy hoặc Base(std::move(other)) để move; bỏ mem-initializer đó sẽ thử default-initialize base thay vì copy hoặc move. Member cũng phải được xử lý theo ownership semantics, còn thứ tự khởi tạo thật vẫn là virtual base, direct base rồi member theo thứ tự declaration bất kể thứ tự chữ trong mem-initializer.
Hãy liên hệ trực tiếp kết luận với kiểu, lifetime, ownership, overload resolution hoặc library contract xuất hiện trong câu hỏi. Nếu kết quả phụ thuộc implementation hoặc vi phạm precondition, cần nói rõ thay vì chỉ đoán output.