This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.
Index Nav: | [Date Index] [Subject Index] [Author Index] [Thread Index] | |
---|---|---|
Message Nav: | [Date Prev] [Date Next] | [Thread Prev] [Thread Next] |
Other format: | [Raw text] |
// found on http://msdn.microsoft.com/en-us/library/dd293665.aspx // MemoryBlock.h #include <iostream> #include <algorithm> #include <vector> using namespace std; class MemoryBlock { public: // Simple constructor that initializes the resource. explicit MemoryBlock(size_t length) : _length(length) , _data(new int[length]) { cout << "In MemoryBlock(size_t). length = " << _length << "." << endl; } // Destructor. ~MemoryBlock() { cout << "In ~MemoryBlock(). length = " << _length << "."; if (_data != NULL) { cout << " Deleting resource."; // Delete the resource. delete[] _data; } cout << endl; } // Copy constructor. MemoryBlock(const MemoryBlock& other) : _length(other._length) , _data(new int[other._length]) { cout << "In MemoryBlock(const MemoryBlock&). length = " << other._length << ". Copying resource." << endl; copy(other._data, other._data + _length, _data); } // Copy assignment operator. MemoryBlock& operator=(const MemoryBlock& other) { cout << "In operator=(const MemoryBlock&). length = " << other._length << ". Copying resource." << endl; if (this != &other) { // Free the existing resource. delete[] _data; _length = other._length; _data = new int[_length]; copy(other._data, other._data + _length, _data); } return *this; } // Retrieves the length of the data resource. size_t Length() const { return _length; } // Move constructor. MemoryBlock(MemoryBlock&& other) : _data(NULL) , _length(0) { cout << "In MemoryBlock(MemoryBlock&&). length = " << other._length << ". Moving resource." << endl; // Copy the data pointer and its length from the // source object. _data = other._data; _length = other._length; // Release the data pointer from the source object so that // the destructor does not free the memory multiple times. other._data = NULL; other._length = 0; } // Move assignment operator. MemoryBlock& operator=(MemoryBlock&& other) { cout << "In operator=(MemoryBlock&&). length = " << other._length << "." << endl; if (this != &other) { // Free the existing resource. delete[] _data; // Copy the data pointer and its length from the // source object. _data = other._data; _length = other._length; // Release the data pointer from the source object so that // the destructor does not free the memory multiple times. other._data = NULL; other._length = 0; } return *this; } private: size_t _length; // The length of the resource. int* _data; // The resource. }; int main() { // Create a vector object and add a few elements to it. vector<MemoryBlock> v; v.push_back(MemoryBlock(25)); v.push_back(MemoryBlock(75)); // Insert a new element into the second position of the vector. v.insert(v.begin() + 1, MemoryBlock(50)); }
Attachment:
their_output
Description: Text document
Attachment:
my_output
Description: Text document
Index Nav: | [Date Index] [Subject Index] [Author Index] [Thread Index] | |
---|---|---|
Message Nav: | [Date Prev] [Date Next] | [Thread Prev] [Thread Next] |