45 lines
1.0 KiB
Markdown
45 lines
1.0 KiB
Markdown
# 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) - 返回类总览
|