1.0 KiB
1.0 KiB
Array::operator=
Array& operator=(const Array& other);
Array& operator=(Array&& other) noexcept;
赋值运算符,用另一个数组的内容替换当前数组的内容。
拷贝赋值(=):
- 先销毁当前所有元素
- 分配与
other相同大小的内存 - 拷贝
other中所有元素
移动赋值(=):
- 先销毁当前所有元素
- 接管
other的所有资源(数据指针、容量、分配器) - 将
other置为空状态
参数:
other- 源数组
返回: 引用自身(*this)
异常:
- 拷贝赋值:
other元素拷贝可能抛出异常
线程安全: ❌ 赋值期间不可并发访问
示例:
XCEngine::Containers::Array<int> arr1 = {1, 2, 3};
XCEngine::Containers::Array<int> arr2;
arr2 = arr1; // 拷贝赋值,arr2 现在是 {1, 2, 3}
XCEngine::Containers::Array<int> arr3 = {4, 5};
arr2 = std::move(arr3); // 移动赋值,arr2 现在是 {4, 5},arr3 为空
相关文档
- Array 总览 - 返回类总览