fork download
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. class A
  6. {
  7. public:
  8.  
  9. A(const std::string& text = "0") : m_text(text)
  10. {
  11.  
  12. std::cout << "A()" << m_text << std::endl;
  13. }
  14.  
  15. A(const A& a) : m_text(a.m_text)
  16. {
  17. std::cout << "A::copy()" << m_text << std::endl;
  18. }
  19.  
  20. A(const A&& a) : m_text(a.m_text)
  21. {
  22. std::cout << "A::copy&&()" << m_text << std::endl;
  23. }
  24.  
  25. A& operator=(const A& a)
  26. {
  27. std::cout << "A::operator=() from " << a.m_text << " to " << m_text << std::endl;
  28.  
  29. m_text = a.m_text;
  30.  
  31. char c;
  32.  
  33. // std::string test(c);
  34.  
  35. std::to_string(c);
  36.  
  37. return *this;
  38. }
  39.  
  40. A& operator=(A&& a)
  41. {
  42. std::cout << "A::operator=&&() from " << a.m_text << " to " << m_text << std::endl;
  43.  
  44. //std::swap(&m_text, &a.m_text);
  45.  
  46. m_text.swap(a.m_text);
  47.  
  48. return *this;
  49. }
  50.  
  51. virtual ~A()
  52. {
  53. std::cout << "~A()" << m_text << std::endl;
  54. }
  55.  
  56. std::string m_text;
  57. };
  58.  
  59. A test()
  60. {
  61. //A a("1");
  62. A a;
  63.  
  64. a.m_text = "4";
  65.  
  66. return a;
  67. //return std::move(a);
  68. }
  69.  
  70. int main()
  71. {
  72. //A a(test());
  73. A a("2");
  74. a = test();
  75.  
  76. a.m_text = "3";
  77.  
  78. return 0;
  79. }
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
A()2
A()0
A::operator=&&() from 4 to 2
~A()2
~A()3