Guest User

http://stackoverflow.com/questions/28623287/using-stdmove-to

a guest
Feb 20th, 2015
364
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.48 KB | None | 0 0
  1. struct A
  2. {
  3.     std::vector<int> x;
  4.     A()
  5.     {
  6.         std::cout << "A()" << std::endl;
  7.     }
  8.    
  9.     A(const A&a)
  10.     {
  11.         std::cout << "A(const A&)" << std::endl;
  12.         x = a.x;
  13.     }
  14.  
  15.     A(A&&a)
  16.     {
  17.         std::cout << "A(A&&)" << std::endl;
  18.         x = std::move(a.x);
  19.     }
  20.  
  21.     ~A()
  22.     {
  23.         std::cout << "~A()" << std::endl;
  24.     }
  25. };
  26.  
  27. struct B : public A
  28. {
  29.     std::vector<int> y;
  30.  
  31.     B()
  32.     {
  33.         std::cout << "B()" << std::endl;
  34.     }
  35.  
  36.     B(const A&a)
  37.     {
  38.         std::cout << "B(const A&)" << std::endl;
  39.         x = a.x;
  40.         y.resize(x.size());
  41.     }
  42.  
  43.     B(A&&a) :A(std::move(a))
  44.     {
  45.         std::cout << "B(A&&)" << std::endl;
  46.         std::cout << "a.x.size=" << a.x.size() << std::endl;
  47.         std::cout << "x.size=" << x.size() << std::endl;
  48.         y.resize(x.size());
  49.     }
  50.  
  51.     B(const B&)
  52.     {
  53.         std::cout << "B(const B&)" << std::endl;
  54.     }
  55.  
  56.     ~B()
  57.     {
  58.         std::cout << "~B()" << std::endl;
  59.     }
  60. };
  61.  
  62. A ret_a()
  63. {
  64.     A a;
  65.     a.x.resize(10);
  66.     return a;
  67. }
  68.  
  69. int _tmain(int argc, _TCHAR* argv[])
  70. {
  71.     std::cout << "section I" << std::endl << std::endl;
  72.    
  73.     A a = ret_a(); 
  74.     B b(a);
  75.     std::cout << "a.x.size=" << a.x.size() << std::endl;
  76.    
  77.     std::cout << std::endl << "section II" << std::endl << std::endl;
  78.    
  79.     B b2(ret_a());
  80.     std::cout << "b.x.size=" << b.x.size() << std::endl;
  81.    
  82.     std::cout << std::endl << "section III" << std::endl << std::endl;
  83.  
  84.     return 0;
  85. }
  86.  
  87. /* Output:
  88.  
  89. section I
  90.  
  91. A()
  92. A()
  93. B(const A&)
  94. a.x.size=10
  95.  
  96. section II
  97.  
  98. A()
  99. A(A&&)
  100. B(A&&)
  101. a.x.size=0
  102. x.size=10
  103. ~A()
  104. b.x.size=10
  105.  
  106. section III
  107.  
  108. ~B()
  109. ~A()
  110. ~B()
  111. ~A()
  112. ~A()
  113.  
  114. */
Add Comment
Please, Sign In to add comment