Review this generated owning packet type and its factory.
Implement a move-only Packet that owns a dynamic byte array and can be returned efficiently from load_packet.
C++
class Packet {
public:
explicit Packet(std::size_t size)
: data_(new unsigned char[size]), size_(size) {}
~Packet() { delete[] data_; }
Packet(const Packet&) = delete;
Packet& operator=(const Packet&) = delete;
Packet(Packet&& other) noexcept
: data_(other.data_), size_(other.size_) {}
Packet& operator=(Packet&& other) noexcept {
delete[] data_;
data_ = other.data_;
size_ = other.size_;
other.data_ = nullptr;
other.size_ = 0;
return *this;
}
private:
unsigned char* data_;
std::size_t size_;
};
Packet load_packet(std::size_t size) {
Packet packet(size);
return std::move(packet);
}
generated code is illustrative, not from any one model