#include <iostream>
#include <string>
class MyClass
{
public:
MyClass()
{
std::cout << "Calling MyClass() ..." << std::endl;
Count++;
}
MyClass(const MyClass &src)
{
std::cout << "Calling MyClass(const MyClass &src) ..." << std::endl;
Count++;
}
MyClass &operator=(const MyClass &rhs)
{
std::cout << "Calling MyClass &operator=(const MyClass &rhs) ..." << std::endl;
return *this;
}
MyClass(MyClass &&src)
{
_Name = std::move(src._Name);
std::cout << "Calling MyClass(MyClass &&src) ..." << std::endl;
}
MyClass &operator=(MyClass &&rhs)
{
_Name = std::move(rhs._Name);
std::cout << "Calling MyClass &operator=(MyClass &&rhs) ..." << std::endl;
return *this;
}
~MyClass()
{
std::cout << "Calling " << _Name << ".~MyClass() ..." << std::endl;
Count--;
}
static int Count;
void Name(std::string name)
{
_Name = name;
}
private:
std::string _Name;
};
int MyClass::Count = 0;
void fun1()
{
std::cout << std::endl
<< "---------- Start fun1() ----------" << std::endl;
std::cout << "Now, Count = " << MyClass::Count << std::endl
<< std::endl;
MyClass mc10;
mc10.Name("mc10");
std::cout << "After 'MyClass mc10;'" << std::endl;
std::cout << " Count = " << MyClass::Count << std::endl;
std::cout << std::endl;
MyClass *mc11 = new MyClass;
mc11->Name("mc11");
std::cout << "After 'MyClass *mc11 = new MyClass;'" << std::endl;
std::cout << " Count = " << MyClass::Count << std::endl;
std::cout << std::endl;
delete mc11;
std::cout << std::endl;
std::cout << "Now, Count = " << MyClass::Count << std::endl;
std::cout << "---------- End fun1() ----------" << std::endl
<< std::endl;
}
MyClass fun2()
{
std::cout << std::endl
<< "---------- Start fun2() ----------" << std::endl;
std::cout << "Now, Count = " << MyClass::Count << std::endl
<< std::endl;
MyClass mc20;
mc20.Name("mc20");
std::cout << "After 'MyClass mc20;'" << std::endl;
std::cout << " Count = " << MyClass::Count << std::endl;
std::cout << std::endl;
std::cout << "Now, Count = " << MyClass::Count << std::endl;
std::cout << "---------- End fun2() ----------" << std::endl;
return mc20;
}
MyClass &fun3()
{
std::cout << std::endl
<< "---------- Start fun3() ----------" << std::endl;
std::cout << "Now, Count = " << MyClass::Count << std::endl
<< std::endl;
MyClass *mc30 = new MyClass;
mc30->Name("mc30");
std::cout << "After 'MyClass *mc30 = new MyClass;'" << std::endl;
std::cout << " Count = " << MyClass::Count << std::endl;
std::cout << std::endl;
std::cout << "Now, Count = " << MyClass::Count << std::endl;
std::cout << "---------- End fun3() ----------" << std::endl;
return *mc30;
}
MyClass &&fun4()
{
std::cout << std::endl
<< "---------- Start fun4() ----------" << std::endl;
std::cout << "Now, Count = " << MyClass::Count << std::endl
<< std::endl;
MyClass *mc40 = new MyClass;
mc40->Name("mc40");
std::cout << "After 'MyClass *mc40 = new MyClass;'" << std::endl;
std::cout << " Count = " << MyClass::Count << std::endl;
std::cout << std::endl;
std::cout << "Now, Count = " << MyClass::Count << std::endl;
std::cout << "---------- End fun4() ----------" << std::endl;
return std::move(*mc40);
}
int main()
{
MyClass mc1;
mc1.Name("mc1");
std::cout << "After 'MyClass mc1;'" << std::endl;
std::cout << " Count = " << MyClass::Count << std::endl;
std::cout << std::endl;
MyClass mc2(mc1);
mc2.Name("mc2");
std::cout << "After 'MyClass mc2(mc1);'" << std::endl;
std::cout << " Count = " << MyClass::Count << std::endl;
std::cout << std::endl;
MyClass mc3 = mc2;
mc3.Name("mc3");
std::cout << "After 'MyClass mc3 = mc2;'" << std::endl;
std::cout << " Count = " << MyClass::Count << std::endl;
std::cout << std::endl;
MyClass mc4;
mc4.Name("mc4");
mc4 = mc3;
std::cout << "After 'mc4 = mc3;'" << std::endl;
std::cout << " Count = " << MyClass::Count << std::endl;
std::cout << std::endl;
MyClass mc5;
mc5.Name("mc5");
std::cout << "After 'MyClass mc5;'" << std::endl;
std::cout << " Count = " << MyClass::Count << std::endl;
std::cout << std::endl;
MyClass mc6(std::move(mc5));
mc6.Name("mc6");
std::cout << "After 'MyClass mc6(std::move(mc5));'" << std::endl;
std::cout << " Count = " << MyClass::Count << std::endl;
std::cout << std::endl;
MyClass mc7 = std::move(mc6);
mc7.Name("mc7");
std::cout << "After 'MyClass mc7 = std::move(mc6);'" << std::endl;
std::cout << " Count = " << MyClass::Count << std::endl;
std::cout << std::endl;
MyClass mc8;
mc8.Name("mc8");
mc8 = std::move(mc7);
std::cout << "After 'mc8 = std::move(mc7);'" << std::endl;
std::cout << " Count = " << MyClass::Count << std::endl;
std::cout << std::endl;
MyClass *mc9 = new MyClass;
mc9->Name("mc9");
std::cout << "After 'MyClass *mc9 = new MyClass;'" << std::endl;
std::cout << " Count = " << MyClass::Count << std::endl;
std::cout << std::endl;
delete mc9;
fun1();
MyClass mc21 = fun2();
std::cout << std::endl;
std::cout << "After calling fun2(), Count = " << MyClass::Count << std::endl;
std::cout << "----------------" << std::endl
<< std::endl;
MyClass &mc31 = fun3();
std::cout << std::endl;
std::cout << "After calling fun3(), Count = " << MyClass::Count << std::endl;
std::cout << "----------------" << std::endl
<< std::endl;
MyClass &&mc41 = fun4();
std::cout << std::endl;
std::cout << "After calling fun4(), Count = " << MyClass::Count << std::endl;
std::cout << "----------------" << std::endl
<< std::endl;
return EXIT_SUCCESS;
}
Calling MyClass() ...
After 'MyClass mc1;'
Count = 1
Calling MyClass(const MyClass &src) ...
After 'MyClass mc2(mc1);'
Count = 2
Calling MyClass(const MyClass &src) ...
After 'MyClass mc3 = mc2;'
Count = 3
Calling MyClass() ...
Calling MyClass &operator=(const MyClass &rhs) ...
After 'mc4 = mc3;'
Count = 4
Calling MyClass() ...
After 'MyClass mc5;'
Count = 5
Calling MyClass(MyClass &&src) ...
After 'MyClass mc6(std::move(mc5));'
Count = 5
Calling MyClass(MyClass &&src) ...
After 'MyClass mc7 = std::move(mc6);'
Count = 5
Calling MyClass() ...
Calling MyClass &operator=(MyClass &&rhs) ...
After 'mc8 = std::move(mc7);'
Count = 6
Calling MyClass() ...
After 'MyClass *mc9 = new MyClass;'
Count = 7
Calling mc9.~MyClass() ...
---------- Start fun1() ----------
Now, Count = 6
Calling MyClass() ...
After 'MyClass mc10;'
Count = 7
Calling MyClass() ...
After 'MyClass *mc11 = new MyClass;'
Count = 8
Calling mc11.~MyClass() ...
Now, Count = 7
---------- End fun1() ----------
Calling mc10.~MyClass() ...
---------- Start fun2() ----------
Now, Count = 6
Calling MyClass() ...
After 'MyClass mc20;'
Count = 7
Now, Count = 7
---------- End fun2() ----------
After calling fun2(), Count = 7
----------------
---------- Start fun3() ----------
Now, Count = 7
Calling MyClass() ...
After 'MyClass *mc30 = new MyClass;'
Count = 8
Now, Count = 8
---------- End fun3() ----------
After calling fun3(), Count = 8
----------------
---------- Start fun4() ----------
Now, Count = 8
Calling MyClass() ...
After 'MyClass *mc40 = new MyClass;'
Count = 9
Now, Count = 9
---------- End fun4() ----------
After calling fun4(), Count = 9
----------------
Calling mc20.~MyClass() ...
Calling mc7.~MyClass() ...
Calling .~MyClass() ...
Calling .~MyClass() ...
Calling .~MyClass() ...
Calling mc4.~MyClass() ...
Calling mc3.~MyClass() ...
Calling mc2.~MyClass() ...
Calling mc1.~MyClass() ...