Phần 1
Core Idea
A moved-from object is an object whose resources may have been transferred to another object.
The moved-from object still exists, but its previous value should not be assumed.
Phần 2
Example
T target = std::move(source);
After the move:
target may own the transferred resources.
source remains valid.
The old value of source is unspecified.
Safe Operations
A moved-from object may safely be:
Assigned a new value
Destroyed
Reinitialized
Used only when the operation does not depend on its previous value
Common Mistakes
Assuming the old value is unchanged
Using the moved-from value in program logic
Moving an object before its value is no longer needed
Treating a moved-from object as destroyed
Practical Rule
After moving from an object, either assign it a new value or stop depending on its stored data.
Key Takeaway
A moved-from object remains valid, but its previous value should not be trusted.Mã mẫu hoàn chỉnh
cpp11/24_moved-from object/main.cpp
#include <iostream>
#include <string>
#include <utility>
int main()
{
std::string source = "BTCUSD";
std::string target = std::move(source);
std::cout << target << '\n';
source = "ETHUSD";
std::cout << source << '\n';
}Tự kiểm tra · không chấm điểm
Bạn đã hiểu những điểm nào?
Bài này chưa có danh sách tự kiểm tra trong nguồn.