Created
June 29, 2020 17:35
-
-
Save vicapow/f5cc3c81ef1a9bce4a348d5ee4e2d974 to your computer and use it in GitHub Desktop.
C++ rvalue reference and move semantics
This file contains 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 <vector> | |
using namespace std; | |
struct A { | |
int * ptr; | |
A() { | |
cout << "Constructor" << endl; | |
ptr = new int; | |
} | |
A(const A & a1) { | |
cout << "Copy Constructor" << endl; | |
this->ptr = new int; | |
} | |
A(A && a1) { | |
cout << "Move Constructor" << endl; | |
this->ptr = a1.ptr; | |
a1.ptr = nullptr; | |
} | |
~A() { | |
cout << "Destructor" << endl; | |
delete ptr; | |
} | |
}; | |
int main(void) { | |
vector<A> v1; | |
A a1; | |
v1.push_back(move(a1)); | |
cout << "added to array" << endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment