#include <iostream>
#include <vector>
#include <thread>
using namespace std;
class Test {
public:
Test(string& label_) :label(label_){}
Test(string&& label_):label(move(label_)){}
// copy constructor
Test(const Test& other) :label(other.label)
{
cout << "copy constructor" << endl;
}
// move constructor
Test(Test&& other) :label(move(other.label))
{
cout << "move constructor" << endl;
}
// copy assign
Test& operator=(const Test& other)
{
cout << "copy assign" << endl;
label = other.label;
return *this;
}
// move assign
Test& operator=(Test&& other) noexcept
{
cout << "move assign" << endl;
label = move(other.label);
return *this;
}
void reset(const Test& other)
{
new(this) Test(other);
}
string getLabel()
{
return label;
}
private:
string label;
};
int main()
{
Test firstTest("extra juicy");
Test secondTest(firstTest);
Test thirdTest(move(firstTest));
// copy assign
secondTest = thirdTest;
// move assign
firstTest = move(thirdTest);
std::cout << "firstTest address:" << &firstTest << std::endl;
std::cout << "firstTest:" << firstTest.getLabel() << std::endl;
std::cout << "**************************************************" << std::endl;
//placement new
Test* ptr = new(&firstTest) Test("placement new");
std::cout << "ptr address: " << &(*ptr) << std::endl;
std::cout << "ptr:" << ptr->getLabel() << std::endl;
std::cout << "**************************************************" << std::endl;
// reset
Test testReset("test reset");
ptr->reset(testReset);
std::cout << "ptr address: " << &(*ptr) << std::endl;
std::cout << "ptr:" << ptr->getLabel() << std::endl;
return 0;
}