Files
XCEngine/docs/api/containers/array/operator-assign.md

45 lines
1.0 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# Array::operator=
```cpp
Array& operator=(const Array& other);
Array& operator=(Array&& other) noexcept;
```
赋值运算符,用另一个数组的内容替换当前数组的内容。
**拷贝赋值(`=`**
- 先销毁当前所有元素
- 分配与 `other` 相同大小的内存
- 拷贝 `other` 中所有元素
**移动赋值(`=`**
- 先销毁当前所有元素
- 接管 `other` 的所有资源(数据指针、容量、分配器)
-`other` 置为空状态
**参数:**
- `other` - 源数组
**返回:** 引用自身(`*this`
**异常:**
- 拷贝赋值:`other` 元素拷贝可能抛出异常
**线程安全:** ❌ 赋值期间不可并发访问
**示例:**
```cpp
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 总览](array.md) - 返回类总览