// Error: la asignación libera primero. Con a = a, destruye los datos que iba a copiar.
#include <cstddef>
#include <iostream>

class IntBuffer {
public:
    explicit IntBuffer(std::size_t size) : data_(new int[size]()), size_(size) {}
    ~IntBuffer() {
        delete[] data_;
    }
    int& at(std::size_t index) {
        return data_[index];
    }
    IntBuffer(const IntBuffer& other) : data_(new int[other.size_]), size_(other.size_) {
        for (std::size_t i = 0; i < size_; i++) {
            data_[i] = other.data_[i];
        }
    }
    IntBuffer& operator=(const IntBuffer& other) {
        delete[] data_;
        data_ = new int[other.size_];
        size_ = other.size_;
        for (std::size_t i = 0; i < size_; i++) {
            data_[i] = other.data_[i];
        }
        return *this;
    }

private:
    int* data_;
    std::size_t size_;
};

int main() {
    IntBuffer a(3);
    a.at(0) = 10;
    std::cout << "antes: " << a.at(0) << "\n";
    IntBuffer& same = a;
    a = same;
    std::cout << "después: " << a.at(0) << "\n";
    return 0;
}
