fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <cstring>
  4. using namespace std;
  5.  
  6. class A
  7. {
  8. public:
  9. A(const char *str)
  10. {
  11. cptr = new char [strlen(str)];
  12. strcpy(cptr,str);
  13. cout<<"A(const char *str) called and cptr is "<<cptr<<endl;
  14. }
  15. A(const A &a)
  16. {
  17. cptr = new char [strlen(a.cptr)+1];
  18. strcpy(this->cptr,a.cptr);
  19. cptr[strlen(a.cptr)] = '$';
  20. cptr[strlen(a.cptr)+1] = '\0';
  21. cout<<"A(const A &a) called and cptr is "<<cptr<<endl;
  22. }
  23. ~A()
  24. {
  25. cout<<"~A() called and cptr is "<<cptr<<endl;
  26. delete [] cptr;
  27. }
  28. private:
  29. char *cptr;
  30. };
  31.  
  32.  
  33. int main()
  34. {
  35. A a1("1"),a2("2"),a3("3");
  36. vector<A> Avec;
  37. Avec.reserve(256);
  38.  
  39. Avec.push_back(a1);
  40. Avec.push_back(a2);
  41. Avec.push_back(a3);
  42. }
Success #stdin #stdout 0s 3036KB
stdin
Standard input is empty
stdout
A(const char *str) called and cptr is 1
A(const char *str) called and cptr is 2
A(const char *str) called and cptr is 3
A(const A &a) called and cptr is 1$
A(const A &a) called and cptr is 2$
A(const A &a) called and cptr is 3$
~A() called and cptr is 1$
~A() called and cptr is 2$
~A() called and cptr is 3$
~A() called and cptr is 3
~A() called and cptr is 2
~A() called and cptr is 1