Last active
July 27, 2017 11:28
-
-
Save odeblic/bf3c68600e5b9f843873a005c12f7b1c to your computer and use it in GitHub Desktop.
Several use cases around move semantic
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 <mutex> | |
struct Object | |
{ | |
Object(int id) : id(id) | |
{ | |
std::cout << '[' << id << "] constructor" << std::endl; | |
} | |
Object(Object const & other) : id(other.id) | |
{ | |
std::cout << '[' << id << "] copy constructor" << std::endl; | |
} | |
Object(Object && other) : id(other.id) | |
{ | |
std::cout << '[' << id << "] move constructor" << std::endl; | |
other.id = 0; | |
} | |
Object operator=(Object const & other) | |
{ | |
std::cout << '[' << id << " -> " << other.id << "] copy constructor" << std::endl; | |
id = other.id; | |
} | |
Object operator=(Object && other) | |
{ | |
std::cout << '[' << id << " -> " << other.id << "] move assignment operator" << std::endl; | |
id = other.id; | |
other.id = 0; | |
} | |
~Object() | |
{ | |
std::cout << '[' << id << "] destructor" << std::endl; | |
} | |
int id; | |
}; | |
Object obj1(100); | |
Object const obj2(101); | |
int main() | |
{ | |
Object obj3(1); | |
Object obj4 = obj1; | |
Object obj5 = Object(3); | |
Object obj6 = std::move(Object(4)); | |
obj1 = obj7; | |
obj2 = std::move(obj4); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment