Skip to content

Instantly share code, notes, and snippets.

@vicapow
Created June 29, 2020 17:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vicapow/f5cc3c81ef1a9bce4a348d5ee4e2d974 to your computer and use it in GitHub Desktop.
Save vicapow/f5cc3c81ef1a9bce4a348d5ee4e2d974 to your computer and use it in GitHub Desktop.
C++ rvalue reference and move semantics
#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