Skip to content

Instantly share code, notes, and snippets.

@FONQRI
Created August 28, 2018 20:29
Show Gist options
  • Save FONQRI/936ba788a5faff024cc4f1db7084db99 to your computer and use it in GitHub Desktop.
Save FONQRI/936ba788a5faff024cc4f1db7084db99 to your computer and use it in GitHub Desktop.
#include <algorithm>
#include <iostream>
#include <string>
#include <utility>
class TestClass {
public:
int i;
TestClass() : i(100)
{
std::clog << "constructor no param " << i << std::endl;
}
TestClass(int i) : i(i) { std::clog << "constructor " << i << std::endl; }
TestClass(TestClass const &input)
{
this->i = input.i;
std::clog << "copy constructor " << i << std::endl;
}
TestClass &operator=(const TestClass &other) // copy assignment
{
if (this != &other) { // self-assignment check expected
this->i = other.i;
}
std::clog << "copy assignment " << i << std::endl;
return *this;
}
TestClass &operator=(const int &other) // copy assignment
{
this->i = other;
std::clog << "int copy assignment " << i << std::endl;
return *this;
}
TestClass &operator=(TestClass &&other) noexcept // move assignment
{
if (this != &other) { // no-op on self-move-assignment
this->i = std::move(other.i);
}
std::clog << "no-op on self-move-assignment " << i << std::endl;
return *this;
}
~TestClass() { std::clog << "destructor " << i << std::endl; }
};
class TestClass2 {
public:
TestClass tc;
TestClass2() : tc(36) {}
TestClass2(bool test)
{
tc = 340;
std::clog << test << std::endl;
}
};
int main()
{
std::clog << "****************** create" << std::endl;
TestClass t1(0);
TestClass t2{1};
std::clog << "****************** copy" << std::endl;
TestClass t3(2);
TestClass t4{t3};
std::clog << "****************** assign" << std::endl;
TestClass t5;
TestClass t6(5);
t5 = t6;
std::clog << "****************** move" << std::endl;
TestClass t7(6);
TestClass t8(7);
std::clog << "t8.i before: " << t8.i << std::endl;
std::clog << "t7.i before: " << t7.i << std::endl;
t7 = std::move(t8);
std::clog << "t8.i after: " << t8.i << std::endl;
std::clog << "t7.i after: " << t7.i << std::endl;
std::clog << "done *******************" << std::endl;
TestClass2 tc2;
TestClass2 tc3(true);
std::clog << "done main*******************" << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment