#include <iostream>
#include <cstdint>
#include <utility> // Required for std::move
using namespace std;
class Buffer {
private:
uint8_t* data;
int size;
public:
// Constructor
Buffer(int n) : data(new uint8_t[n]), size(n) {}
// 1. Explicitly DISABLE Copy Constructor
Buffer(const Buffer& other) = delete;
// 2. Explicitly DISABLE Copy Assignment
Buffer& operator=(const Buffer& other) = delete;
// 3. Implement Move Assignment Operator
// Uses an rvalue reference (Buffer&&)
Buffer& operator=(Buffer&& other) noexcept {
// Guard against self-assignment
if (this != &other) {
// Deallocate the current object's resource
delete[] data;
// Copy the pointer and size from the source (other)
data = other.data;
size = other.size;
// Reset the source object to a "valid but empty" state
other.data = nullptr;
other.size = 0;
}
return *this;
}
// Destructor
~Buffer() {
delete[] data;
}
void set(int index, uint8_t value) {
if (data) {
data[index] = value;
}
}
void print() const {
if (data == nullptr || size == 0) {
cout << "No data";
} else {
for (int i = 0; i < size; i++) {
cout << (int)data[i];
if (i != size - 1) cout << " ";
}
}
cout << endl;
}
};
int main() {
int N, M;
// Process Object A
if (!(cin >> N)) return 0;
Buffer A(N);
for (int i = 0; i < N; i++) {
int temp;
cin >> temp;
A.set(i, (uint8_t)temp);
}
// Process Object B
if (!(cin >> M)) return 0;
Buffer B(M);
for (int i = 0; i < M; i++) {
int temp;
cin >> temp;
B.set(i, (uint8_t)temp);
}
// 4. Use std::move to transfer ownership from B to A
A = std::move(B);
A.print();
B.print();
return 0;
}
Input
4 10 20 30 40 3 1 2 3
Expected Output
1 2 3 No data