Created
October 22, 2017 15:53
-
-
Save utilForever/f8c8775c914e2e4722b69624afba28c8 to your computer and use it in GitHub Desktop.
Rvalue reference, move semantics, std::move example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <algorithm> | |
class A | |
{ | |
public: | |
// Simple constructor that initializes the resource. | |
explicit A(size_t length) | |
: mLength(length), mData(new int[length]) | |
{ | |
std::cout << "A(size_t). length = " | |
<< mLength << "." << std::endl; | |
} | |
// Destructor. | |
~A() | |
{ | |
std::cout << "~A(). length = " << mLength << "."; | |
if (mData != NULL) { | |
std::cout << " Deleting resource."; | |
delete[] mData; // Delete the resource. | |
} | |
std::cout << std::endl; | |
} | |
// Copy constructor. | |
A(const A& other) | |
: mLength(other.mLength), mData(new int[other.mLength]) | |
{ | |
std::cout << "A(const A&). length = " | |
<< other.mLength << ". Copying resource." << std::endl; | |
std::copy(other.mData, other.mData + mLength, mData); | |
} | |
// Copy assignment operator. | |
A& operator=(const A& other) | |
{ | |
std::cout << "operator=(const A&). length = " | |
<< other.mLength << ". Copying resource." << std::endl; | |
if (this != &other) { | |
delete[] mData; // Free the existing resource. | |
mLength = other.mLength; | |
mData = new int[mLength]; | |
std::copy(other.mData, other.mData + mLength, mData); | |
} | |
return *this; | |
} | |
// Move constructor. | |
A(A&& other) : mData(NULL), mLength(0) | |
{ | |
std::cout << "A(A&&). length = " | |
<< other.mLength << ". Moving resource.\n"; | |
// Copy the data pointer and its length from the | |
// source object. | |
mData = other.mData; | |
mLength = other.mLength; | |
// Release the data pointer from the source object so that | |
// the destructor does not free the memory multiple times. | |
other.mData = NULL; | |
other.mLength = 0; | |
} | |
// Move assignment operator. | |
A& operator=(A&& other) | |
{ | |
std::cout << "operator=(A&&). length = " | |
<< other.mLength << "." << std::endl; | |
if (this != &other) { | |
// Free the existing resource. | |
delete[] mData; | |
// Copy the data pointer and its length from the | |
// source object. | |
mData = other.mData; | |
mLength = other.mLength; | |
// Release the data pointer from the source object so that | |
// the destructor does not free the memory multiple times. | |
other.mData = NULL; | |
other.mLength = 0; | |
} | |
return *this; | |
} | |
// Retrieves the length of the data resource. | |
size_t Length() const | |
{ | |
return mLength; | |
} | |
private: | |
size_t mLength; // The length of the resource. | |
int* mData; // The resource. | |
}; | |
#include <vector> | |
int main() | |
{ | |
std::vector<A> v; | |
A aObj(25); // lvalue | |
v.push_back(std::move(aObj)); //calls push_back(T&&) | |
std::cout << aObj.Length() << "\n"; | |
// Create a vector object and add a few elements to it. | |
v.push_back(A(25)); | |
v.push_back(A(75)); | |
// Insert a new element into the second position of the vector. | |
v.insert(v.begin() + 1, A(50)); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Reference: https://stackoverflow.com/questions/4986673/c11-rvalues-and-move-semantics-confusion-return-statement