fork(1) download
  1. #include <iostream>
  2. #include <cstring>
  3. using namespace std;
  4.  
  5. class String
  6. {
  7. char *buffer;
  8. public:
  9. String()
  10. {
  11. cout << "Constructor 1 "<<this<<endl;
  12. buffer = new char[1];
  13. buffer[0] = '\0';
  14. }
  15. String(const char *str)
  16. {
  17. cout << "Constructor 2 "<<this<<endl;
  18. buffer = new char[strlen(str)+1];
  19. strcpy(buffer,str);
  20. }
  21. String(const String &s)
  22. {
  23. cout << "Constructor copy "<<this<<" "<<&s<<endl;
  24. buffer = new char[strlen(s.buffer)+1];
  25. strcpy(buffer,s.buffer);
  26. }
  27. const char * get() const {return buffer;}
  28. ~String()
  29. {
  30. cout << "destructor "<<this<<endl;
  31. delete [] buffer;
  32. buffer = NULL;
  33. }
  34. String & operator = (const String &s)
  35. {
  36. cout << "operator = "<<this<<" "<<&s<<endl;
  37. if (this == &s) return *this;
  38. delete [] buffer;
  39. buffer = new char[strlen(s.buffer)+1];
  40. strcpy(buffer,s.buffer);
  41. return *this;
  42. }
  43.  
  44. };
  45.  
  46. String func()
  47. {
  48. String s1 = "AAAAAAAAAAAAAAA";
  49. return s1;
  50. }
  51.  
  52. int main()
  53. {
  54. String s1;
  55.  
  56. s1 = func();
  57.  
  58.  
  59. cout<<s1.get()<<endl;
  60.  
  61. return 0;
  62. }
Success #stdin #stdout 0s 3416KB
stdin
Standard input is empty
stdout
Constructor 1 0xbfeb6668
Constructor 2 0xbfeb666c
operator = 0xbfeb6668 0xbfeb666c
destructor 0xbfeb666c
AAAAAAAAAAAAAAA
destructor 0xbfeb6668