test_emplace.cc 571 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #include <vector>
  2. #include <iostream>
  3. using namespace std;
  4. class A
  5. {
  6. int *data;
  7. public:
  8. A(): data(new int[100])
  9. {
  10. cout << "constructing A, data is " << data << endl;
  11. }
  12. A(A &&x):
  13. data(x.data)
  14. {
  15. cout << "moving A, data is " << data << endl;
  16. x.data = nullptr;
  17. }
  18. ~A()
  19. {
  20. if (data)
  21. delete [] data;
  22. }
  23. void set(int i, int x)
  24. {
  25. data[i] = x;
  26. }
  27. void print(int i)
  28. {
  29. cout << data[i] << endl;
  30. }
  31. };
  32. int main()
  33. {
  34. vector<A> va;
  35. va.emplace_back();
  36. va.emplace_back();
  37. va.emplace_back();
  38. va.emplace_back();
  39. va.emplace_back();
  40. va.emplace_back();
  41. }