Advertisement
Timtsa

20.12.2018

Dec 20th, 2018
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.44 KB | None | 0 0
  1. #include <iostream>
  2. #include<cstring>
  3. using namespace std;
  4.  
  5.  
  6.  
  7.  
  8. class _String
  9. {
  10.     char*data;
  11. public:
  12.     _String()
  13.     {
  14.         data = nullptr;
  15.     }
  16.     _String(const char*s)
  17.     {
  18.         size_t size = strlen(s) + 1;
  19.         data = new char[size];
  20.         memcpy(data, s, size);
  21.     }
  22.  
  23.     _String(const _String &other)
  24.     {
  25.         cout << "construct copy\n";
  26.         size_t size = strlen(other.data) + 1;
  27.         data = new char[size];
  28.         memcpy(data, other.data, size);
  29.     }
  30.    
  31.     _String(_String &&other)
  32.     {
  33.          cout<< "Move copy\n";
  34.         swap(data, other.data);
  35.         other.data = nullptr;
  36.     }
  37.    
  38.    
  39.    
  40.     _String & operator = (const _String & other)
  41.     {
  42.         cout << "Operator = \n";
  43.         if (this == &other) return *this;
  44.         {
  45.             data = new char[strlen(other.data) + 1];
  46.             memcpy(data, other.data, strlen(other.data) + 1);
  47.             return*this;
  48.         }
  49.     }
  50.  
  51.     _String operator = (_String && other)
  52.     {
  53.         cout << "Move \n";
  54.         swap(data, other.data);
  55.         return *this;
  56.     }
  57.  
  58.     _String  operator + (const _String & other)
  59.     {
  60.         cout << "operat + \n";
  61.         _String temp;
  62.         temp.data = new char[strlen(data) + strlen(other.data) + 1];
  63.         memcpy(temp.data, data, strlen(data));
  64.         memcpy(temp.data + strlen(data), other.data, strlen(other.data) + 1);
  65.         return temp;
  66.     }
  67.     void Print()
  68.     {
  69.         cout << data << endl;
  70.     }
  71.  
  72.     ~_String()
  73.     {
  74.         cout << "deletor\n";
  75.         delete[] data;
  76.  
  77.     }
  78. };
  79.  
  80. void main()
  81. {
  82.     _String s1("Hello "), s2("world"), s3;
  83.     s3 = s1 + s2;
  84.     s3.Print();
  85.     _String s4(s3);
  86.     system("Pause");
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement