Allow move assignment for Data class

This commit is contained in:
Sebastian Messmer 2015-04-25 16:44:24 +02:00
parent 40451e762c
commit dcc979d65f
2 changed files with 15 additions and 1 deletions

View File

@ -24,6 +24,17 @@ Data::Data(Data &&rhs)
: _size(rhs._size), _data(rhs._data) { : _size(rhs._size), _data(rhs._data) {
// Make rhs invalid, so the memory doesn't get freed in its destructor. // Make rhs invalid, so the memory doesn't get freed in its destructor.
rhs._data = nullptr; rhs._data = nullptr;
rhs._size = 0;
}
Data &Data::operator=(Data &&rhs) {
std::free(_data);
_data = rhs._data;
_size = rhs._size;
rhs._data = nullptr;
rhs._size = 0;
return *this;
} }
Data::~Data() { Data::~Data() {

View File

@ -13,9 +13,12 @@ namespace cpputils {
class Data { class Data {
public: public:
explicit Data(size_t size); explicit Data(size_t size);
Data(Data &&rhs); // move constructor
virtual ~Data(); virtual ~Data();
Data(Data &&rhs); // move constructor
//TODO Test move assignment (and if not tested yet, also move constructor)
Data &operator=(Data &&rhs); // move assignment
Data copy() const; Data copy() const;
void *data(); void *data();