fork download
  1. #include <iostream>
  2. #include<string.h>
  3. using namespace std;
  4. class my_string {
  5. private:
  6. char* m_data;
  7. unsigned length;
  8. public:
  9. my_string(const char* m){
  10. cout<<"ctor"<<endl;
  11. length = strlen(m);
  12. m_data = new char[length];
  13. memcpy(m_data, m, length);
  14. }
  15. my_string(const my_string& other){
  16. cout<<"copy ctor"<<endl;
  17. length = other.length;
  18. m_data = new char[length];
  19. memcpy(m_data, other.m_data, length);
  20. }
  21. my_string(my_string&& other) noexcept {
  22. cout<<"move ctor"<<endl;
  23. length = other.length;
  24. m_data = other.m_data;
  25. other.length = 0;
  26. other.m_data = nullptr;
  27. }
  28. ~my_string(){
  29. cout<<"dtor"<<endl;
  30. length = 0;
  31. delete m_data;
  32. }
  33. char* c_str(){ return m_data;}
  34. };
  35.  
  36. class Entity{
  37. private:
  38. my_string str;
  39. public:
  40. Entity(const my_string& s): str(s){}
  41. Entity(my_string&& s):str(std::move(s)) {
  42. //str = std::move(s);
  43. }
  44.  
  45. ~Entity(){
  46.  
  47. }
  48. void print(){
  49. cout<< str.c_str()<<endl;
  50. }
  51. };
  52. int main() {
  53. //Entity E(my_string("priya"));
  54. //my_string p ("priya");
  55. //my_string q = p;
  56. my_string* p = new my_string("pl");
  57. my_string y = std::move("yadav");
  58. // E.print();
  59. // your code goes here
  60. return 0;
  61. }
Success #stdin #stdout 0.01s 5508KB
stdin
Standard input is empty
stdout
ctor
ctor
dtor