Last active
April 19, 2023 04:40
-
-
Save Loki-Astari/489bf3009116b8f5821c750995006a17 to your computer and use it in GitHub Desktop.
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
> g++ -std=c++17 test.cpp | |
> ./a.out | |
Expect: Construct Normal | |
Construct Normal | |
Expect: Construct Copy | |
Constructor Copy | |
Expect: Construct Move | |
Constructor Move | |
Expect: Construct Default | |
Construct Default | |
Expect: Construct Copy -> Assignment Operator -> Destruct -1 | |
Constructor Copy | |
Assignment Operator | |
Destructor: -1 | |
Expect: Construct Move -> Assignment Operator -> Destruct 5 | |
Constructor Move | |
Assignment Operator | |
Destructor: 5 | |
Destructors Now | |
Expect: Destroy: pDefault 5 | |
Expect: Destroy: pMove 5 | |
Expect: Destroy: pcopy -1 | |
Expect: Destroy: Normal -1 | |
Destructor: 5 | |
Destructor: 5 | |
Destructor: -1 | |
Destructor: -1 |
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> | |
class Test | |
{ | |
int* val; | |
public: | |
Test() | |
: val(nullptr) | |
{ | |
std::cout << "Construct Default\n"; | |
} | |
~Test() | |
{ | |
std::cout << "Destructor: " << (val ? *val : -1) << "\n"; | |
delete val; | |
} | |
explicit Test(int* val) | |
: val(val) | |
{ | |
std::cout << "Construct Normal\n"; | |
} | |
Test(Test const& copy) | |
: val(new int(*copy.val)) | |
{ | |
std::cout << "Constructor Copy\n"; | |
} | |
Test(Test&& move) noexcept | |
: val(std::exchange(move.val, nullptr)) | |
{ | |
std::cout << "Constructor Move\n"; | |
} | |
Test& operator=(Test rhs) noexcept | |
{ | |
std::cout << "Assignment Operator\n"; | |
swap(rhs); | |
return *this; | |
} | |
void swap(Test& rhs) noexcept | |
{ | |
std::swap(val, rhs.val); | |
} | |
}; | |
int main() | |
{ | |
// Construct Normal | |
std::cout << "\nExpect: Construct Normal\n"; | |
Test pNormal(new int(5)); // Normal = 5 | |
std::cout << "\nExpect: Construct Copy\n"; | |
Test pCopy(pNormal); // Copy = 5 | |
std::cout << "\nExpect: Construct Move\n"; | |
Test pMove(std::move(pNormal)); // Move = 5 (Normal = -1) | |
std::cout << "\nExpect: Construct Default\n"; | |
Test pDefault; // Default= -1 | |
std::cout << "\nExpect: Construct Copy -> Assignment Operator -> Destruct -1\n"; | |
pDefault = pCopy; // Default= 5 | |
std::cout << "\nExpect: Construct Move -> Assignment Operator -> Destruct 5\n"; | |
pDefault = std::move(pCopy); // Default= 5 (Copy = -1) | |
std::cout << "\n\nDestructors Now\n"; | |
std::cout << "Expect: Destroy: pDefault 5\n"; | |
std::cout << "Expect: Destroy: pMove 5\n"; | |
std::cout << "Expect: Destroy: pcopy -1\n"; | |
std::cout << "Expect: Destroy: Normal -1\n"; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment