fork download
  1. #include <iostream>
  2. #include <cstring>
  3.  
  4. struct String {
  5. char *buf;
  6. size_t size;
  7.  
  8. String () : buf(0), size(0){}
  9. ~String () {
  10. std::cout << "release " << (void*)buf << std::endl;
  11. delete [] buf;
  12. }
  13. String (char *s) {
  14. size = strlen(s)+1;
  15. buf = new char[size];
  16. strcpy (buf, s);
  17. std::cout << "allocated at " << (void*)buf << std::endl;
  18. }
  19. String (String&& o) : buf(o.buf), size(o.size) { o.buf = 0; o.size = 0; }
  20. String (const String& o) : buf(new char[o.size]), size(o.size) {
  21. strcpy (buf, o.buf);
  22. std::cout << "allocated at " << (void*)buf << std::endl;
  23. }
  24. };
  25.  
  26. void foo (String&& s) {
  27. std::cout << s.buf << std::endl;
  28. }
  29.  
  30. int main() {
  31. foo (String ("test"));
  32.  
  33. String a("test1");
  34. String b(std::move(a));
  35.  
  36. return 0;
  37. }
Success #stdin #stdout 0s 3472KB
stdin
Standard input is empty
stdout
allocated at 0x8531008
test
release 0x8531008
allocated at 0x8531008
release 0x8531008
release 0