ما هي دلالات النقل (move semantics) في C++11 وكيف تعمل؟
مرحباً بالجميع! بدأت دراسة C++11 وأواجه باستمرار مصطلح "move semantics"، لكنني لا أستطيع فهمه تماماً وما هو الغرض منه. قرأت مقالات مختلفة، لكن الأمثلة في كل مكان كانت مجردة نوعاً ما.
لدي هذا الكود. لنفترض أنني أكتب كلاساً يدير مصفوفة ديناميكية:
```cpp
#include
#include
class MyArray {
public:
int* data;
size_t size;
MyArray(size_t s) : size(s) {
data = new int[size];
std::cout << "Constructor called\n";
}
MyArray(const MyArray& other) : size(other.size) {
data = new int[size];
std::memcpy(data, other.data, size * sizeof(int));
std::cout << "Copy constructor called\n";
}
MyArray& operator=(const MyArray& other) {
if (this != &other) {
delete[] data;
size = other.size;
data = new int[size];
std::memcpy(data, other.data, size * sizeof(int));
s