Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.48 KB | None | 0 0
  1. #include <iostream>
  2. #include <string.h>
  3.  
  4. using namespace std;
  5.  
  6. class MyString
  7. {
  8.  
  9. private:
  10. char *str;
  11. int *ref;
  12.  
  13. public:
  14. MyString()
  15. {
  16. cout << "empty" << endl;
  17. ref = nullptr;
  18. cout << "empty" << endl;
  19. }
  20. MyString(const char *init_val)
  21. {
  22. cout << "char*" << endl;
  23. // strcpy(str, init_val);
  24. ref = new int;
  25.  
  26. str = new char[strlen(init_val)];
  27.  
  28. // for (size_t i = 0; i < strlen(init_val); i++)
  29. // {
  30. // str[i] = init_val[i];
  31. // }
  32.  
  33. strcpy(str, init_val);
  34.  
  35.  
  36. *ref = 1;
  37. cout << *ref;
  38. cout << "char*" << endl;
  39. }
  40. MyString(MyString &&rhs)
  41. {
  42. cout << "rhs&&" << endl;
  43. str = rhs.str;
  44. rhs.str = nullptr;
  45. ref = rhs.ref;
  46. rhs.ref = nullptr;
  47. cout << *ref;
  48. cout << "rhs&&" << endl;
  49. }
  50. MyString(const MyString &mystr)
  51. {
  52. cout << "rhs&" << endl;
  53. str = mystr.str;
  54. ref = mystr.ref;
  55. (*ref)++;
  56. cout << *ref;
  57. cout << "rhs&" << endl;
  58. }
  59.  
  60. MyString &operator=(const MyString &rhs)
  61. {
  62. cout << "1" << endl;
  63. if (ref == nullptr)
  64. cout << "NULLLLL" << endl;
  65. else
  66. cout << "2" << endl;
  67.  
  68. str = rhs.str;
  69. cout << "3" << endl;
  70. ref = rhs.ref;
  71. cout << "4" << endl;
  72. (*ref)++;
  73. cout << "5" << endl;
  74. }
  75.  
  76. MyString &operator+=(const MyString &rhs)
  77. {
  78.  
  79. if (*ref == 1)
  80. {
  81. strcat(str, rhs.str);
  82. return *this;
  83. }
  84. else
  85. {
  86. char *temp = str;
  87.  
  88. str = new char[strlen(temp) + strlen(rhs.str)];
  89.  
  90. strcpy(str, temp);
  91. strcat(str, rhs.str);
  92. (*ref)--;
  93. delete[] temp;
  94. return *(new MyString(str));
  95. }
  96. }
  97. MyString &operator+=(const char *rhs)
  98. {
  99. if (*ref == 1)
  100. {
  101. strcat(str, rhs);
  102. return *this;
  103. }
  104. else
  105. {
  106. char *temp = str;
  107.  
  108. str = new char[strlen(temp) + strlen(rhs)];
  109.  
  110. strcpy(str, temp);
  111. strcat(str, rhs);
  112. (*ref)--;
  113. delete[] temp;
  114. return *(new MyString(str));
  115. }
  116. }
  117. MyString &operator<<(const char *rhs)
  118. {
  119. }
  120. MyString &operator>>(const char *rhs)
  121. {
  122. }
  123. char operator[](const int &i) const
  124. {
  125. return str[i];
  126. }
  127.  
  128. int length() const
  129. {
  130. return strlen(str);
  131. }
  132. char *GetString() const
  133. {
  134. return str;
  135. }
  136. int GetRef()
  137. {
  138. return *ref;
  139. }
  140. ~MyString()
  141. {
  142. cout << "Destructor" << endl;
  143. if (*ref == 1)
  144. {
  145. delete[] str;
  146. delete ref;
  147. }
  148. else
  149. {
  150. (*ref)--;
  151. }
  152. }
  153.  
  154. void operator++(int i)
  155. {
  156. str[0] = 'b';
  157. }
  158. };
  159.  
  160. MyString operator+(const MyString &lhs, const MyString &rhs)
  161. {
  162. cout << "++++++" << endl;
  163. char *temp;
  164. strcpy(temp, lhs.GetString());
  165. strcat(temp, rhs.GetString());
  166.  
  167. return MyString(temp);
  168. }
  169. // MyString operator+(MyString &&lhs, MyString &&rhs)
  170. // {
  171. // }
  172. MyString operator+(const MyString &lhs, char *rhs)
  173. {
  174. char *temp;
  175. strcpy(temp, lhs.GetString());
  176. strcat(temp, rhs);
  177.  
  178. return MyString(temp);
  179. }
  180. MyString operator+(char *lhs, const MyString &rhs)
  181. {
  182. char *temp;
  183. strcpy(temp, lhs);
  184. strcat(temp, rhs.GetString());
  185.  
  186. return MyString(temp);
  187. }
  188. // MyString operator+(const char *rhs)
  189. // {
  190. // MyString temp();
  191. // if (*ref == 1)
  192. // {
  193. // strcat(str, rhs);
  194. // return *this;
  195. // }
  196. // else
  197. // {
  198. // char *temp = str;
  199.  
  200. // str = new char[strlen(temp) + strlen(rhs)];
  201.  
  202. // strcpy(str, temp);
  203. // strcat(str, rhs);
  204. // (*ref)--;
  205. // delete[] temp;
  206. // return *(new MyString(str));
  207. // }
  208. // }
  209.  
  210. ostream &operator<<(ostream &os, MyString &mystr)
  211. {
  212. return os << mystr.GetString() << " " << mystr.GetRef();
  213. }
  214. ostream &operator>>(ostream &os, MyString &mystr)
  215. {
  216. }
  217.  
  218. int main()
  219. {
  220. MyString a("aaa");
  221. MyString c(a);
  222. MyString b("bbb");
  223. MyString test;
  224.  
  225. cout << a << endl;
  226. cout << b << endl;
  227. cout << c << endl;
  228.  
  229. test = a + b;
  230.  
  231. a = test;
  232.  
  233. cout << a << endl;
  234. cout << b << endl;
  235. cout << c << endl;
  236.  
  237. cout << test << endl;
  238.  
  239. // MyString a("sssasdasd");
  240. // cout << a.GetRef();
  241. // MyString b(a);
  242. // MyString C(b);
  243. // if (true)
  244. // {
  245. // MyString g = a;
  246. // cout << endl
  247. // << g.GetRef() << endl;
  248. // cout << g << endl;
  249. // }
  250. // cout << a << endl;
  251. // cout << a.GetRef();
  252. // cout << b << endl;
  253.  
  254. // char as[] = "asd";
  255. // char *sss = as;
  256.  
  257. return 0;
  258. // cout << as;
  259.  
  260. // cout << '\n'
  261. // << sss;
  262.  
  263. // sss[2] = '0';
  264.  
  265. // cout << '\n'
  266. // << as;
  267. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement