fork(1) download
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. class A{
  5. public:
  6. A(int i){
  7. std::cout << "Constructor "<< i <<std::endl;
  8. for(int l = 0; l<i;l++){
  9. vec.push_back(l);
  10. }
  11. };
  12.  
  13. A(A && ref): vec(std::move(ref.vec))
  14. {
  15. std::cout << "Move constructor"<<std::endl;
  16. }
  17.  
  18. A & operator=(A && ref){
  19. if(this != &ref){
  20. vec = std::move(ref.vec);
  21. }
  22. std::cout << "Move assignment"<<std::endl;
  23. return *this;
  24.  
  25. }
  26.  
  27. std::vector<int> vec;
  28.  
  29. private:
  30. A(const A & ref);
  31. A(A & ref);
  32. A & operator=(A & ref);
  33. };
  34.  
  35.  
  36. A makeA(){
  37. A a(3);
  38. return a;
  39. }
  40.  
  41. int main(){
  42. A b1(makeA()) ;
  43. A b2 = makeA();
  44. A b3 = A(3);
  45. A b4(A(3));
  46. std::cout << b4.vec[2] << std::endl;
  47. };
Success #stdin #stdout 0s 3476KB
stdin
Standard input is empty
stdout
Constructor 3
Constructor 3
Constructor 3
Constructor 3
2