Last active
March 27, 2021 06:01
-
-
Save uchidev/f80323f45a5592b22e72e9a1d81ac59f to your computer and use it in GitHub Desktop.
C++ Copy and Move
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 <cstring> | |
struct Foo { | |
Foo() { | |
std::cout << "Constructor: " << this << std::endl; | |
array = new char[4096]; | |
} | |
~Foo() { | |
std::cout << "Destructor: " << this << std::endl; | |
delete[] array; | |
} | |
Foo(const Foo& obj) { | |
std::cout << "Copy Constructor: " << this << std::endl; | |
array = new char[4096]; | |
std::memcpy(array, obj.array, 4096); | |
} | |
Foo(Foo&& obj) noexcept { | |
std::cout << "Move Constructor: " << this << std::endl; | |
array = obj.array; | |
obj.array = nullptr; | |
} | |
Foo& operator=(const Foo& obj) { | |
std::cout << "Copy Operator: " << this << " from " << &obj << std::endl; | |
if (this == &obj) { | |
return *this; | |
} | |
// delete[] array; | |
// array = new char[4096]; | |
std::memcpy(array, obj.array, 4096); | |
return *this; | |
} | |
Foo& operator=(Foo&& obj) noexcept { | |
std::cout << "Move Operator: " << this << " from " << &obj << std::endl; | |
if (this == &obj) { | |
return *this; | |
} | |
delete[] array; | |
array = obj.array; | |
obj.array = nullptr; | |
return *this; | |
} | |
char* array = nullptr; | |
}; | |
#pragma clang diagnostic push | |
#pragma ide diagnostic ignored "performance-unnecessary-copy-initialization" | |
#pragma ide diagnostic ignored "UnusedValue" | |
#pragma ide diagnostic ignored "UnusedLocalVariable" | |
int main(int argc, char **argv) { | |
std::cout << "## Foo a, b, c" << std::endl; | |
Foo a, b, c; | |
std::cout << std::endl << "## Foo v(a)" << std::endl; | |
Foo v(a); | |
std::cout << std::endl << "## Foo w = a" << std::endl; | |
Foo w = a; | |
std::cout << std::endl << "## Foo x" << std::endl; | |
Foo x; | |
std::cout << std::endl << "## x = a" << std::endl; | |
x = a; | |
std::cout << std::endl << "## Foo y = std::move(b)" << std::endl; | |
Foo y = std::move(b); | |
std::cout << std::endl << "## Foo z" << std::endl; | |
Foo z; | |
std::cout << std::endl << "## z = std::move(c)" << std::endl; | |
z = std::move(c); | |
std::cout << std::endl << "## z = Foo()" << std::endl; | |
z = Foo(); | |
std::cout << std::endl << "## c = a" << std::endl; | |
c = a; | |
std::cout << std::endl << "## return 0" << std::endl; | |
return 0; | |
} | |
#pragma clang diagnostic pop |
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
## Foo a, b, c | |
Constructor: 004FFEB0 | |
Constructor: 004FFEA4 | |
Constructor: 004FFE98 | |
## Foo v(a) | |
Copy Constructor: 004FFE8C | |
## Foo w = a | |
Copy Constructor: 004FFE80 | |
## Foo x | |
Constructor: 004FFE74 | |
## x = a | |
Copy Operator: 004FFE74 from 004FFEB0 | |
## Foo y = std::move(b) | |
Move Constructor: 004FFE68 | |
## Foo z | |
Constructor: 004FFE5C | |
## z = std::move(c) | |
Move Operator: 004FFE5C from 004FFE98 | |
## z = Foo() | |
Constructor: 004FFE54 | |
Move Operator: 004FFE5C from 004FFE54 | |
Destructor: 004FFE54 | |
## c = a | |
Copy Operator: 004FFE98 from 004FFEB0 | |
## return 0 | |
Destructor: 004FFE5C | |
Destructor: 004FFE68 | |
Destructor: 004FFE74 | |
Destructor: 004FFE80 | |
Destructor: 004FFE8C | |
Destructor: 004FFE98 | |
Destructor: 004FFEA4 | |
Destructor: 004FFEB0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment