fork download
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. class object
  5. {
  6. private:
  7.  
  8. int value;
  9.  
  10. public:
  11.  
  12. object(){}
  13. object(int value)
  14. {
  15. std::cout<<"object::constructor: "<< value << std::endl;
  16. this->value = value;
  17. }
  18. object( const object& o )
  19. {
  20. std::cout<<"object::copy-constructor: " << o.value << std::endl;
  21. this->value = o.value + 10;
  22. }
  23. ~object()
  24. {
  25. std::cout<<"object::destructor: "<< value << std::endl;
  26. }
  27. void call()
  28. {
  29. std::cout<<"object::call(): begin"<<std::endl;
  30. std::cout<<value<<std::endl;
  31. std::cout<<"object::call(): end"<<std::endl;
  32. }
  33. };
  34.  
  35. int main()
  36. {
  37. int max = 3;
  38. std::vector <object> OBJECTS;
  39.  
  40. for(int index = 0; index < max; index++)
  41. {
  42. object OBJECT(index);
  43.  
  44. std::cout<<"before push_back: capacity="<< OBJECTS.capacity() << std::endl;
  45. OBJECTS.push_back(OBJECT);
  46. std::cout<<"after push_back: capacity="<< OBJECTS.capacity() << std::endl;
  47. }
  48.  
  49. for(int index = 0; index < max; index++)
  50. OBJECTS[index].call();
  51.  
  52. return 0;
  53. }
Success #stdin #stdout 0s 2988KB
stdin
Standard input is empty
stdout
object::constructor: 0
before push_back: capacity=0
object::copy-constructor: 0
after push_back: capacity=1
object::destructor: 0
object::constructor: 1
before push_back: capacity=1
object::copy-constructor: 1
object::copy-constructor: 10
object::destructor: 10
after push_back: capacity=2
object::destructor: 1
object::constructor: 2
before push_back: capacity=2
object::copy-constructor: 2
object::copy-constructor: 20
object::copy-constructor: 11
object::destructor: 20
object::destructor: 11
after push_back: capacity=4
object::destructor: 2
object::call(): begin
30
object::call(): end
object::call(): begin
21
object::call(): end
object::call(): begin
12
object::call(): end
object::destructor: 30
object::destructor: 21
object::destructor: 12