Is this class a standard C ++ 0x way to prevent copying and assignment to protect client code from being accidentally double deleted data?
struct DataHolder {
int *data;
DataHolder(const char* fn);
DataHolder(const char* fn, size_t len);
~DataHolder() { delete[] data; }
DataHolder(const DataHolder&) = delete;
DataHolder& operator=(const DataHolder&) = delete;
DataHolder(DataHolder &&other) {
data=other.data; other.data=nullptr;
}
DataHolder& operator=(DataHolder &&other) {
if(&other!=this) { data = other.data; other.data=nullptr};
return *this;
}
};
You noticed that I have defined new methods for moving and moving here. Did I implement them correctly?
Is there any way - using the definition of displacement and displacement - to put DataHolderin a standard container? like a vector? How should I do it?
Interestingly, some options come to mind:
vector<DataHolder> abc { DataHolder("a"), DataHolder("b"), DataHolder("c") };
vector<DataHolder> xyz;
xyz.push_back( DataHolder("x") );
xyz.emplace_back( "z", 1 );
DataHolder y("y");
xyz.push_back( y );
xyz.push_back( move(y) );
xyz.emplace_back( y );
An idea emplace_backis just a guess.
Change . I used the answers in the sample code for reader convenience.