myString.cpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <string>
  3. #include "myString.h"
  4. using namespace std;
  5. // Îïðåäåëåíèå êîíñòðóêòîðà.
  6. MyString::MyString(const char* Str)
  7. {
  8. m_pStr = new char[strlen(Str) + 1];
  9. strcpy_s(m_pStr, strlen(Str) + 1, Str);
  10. }
  11. //ìåòîä , êîòîðûé îáåñïå÷èò äîñòóï ê õðàíÿùåéñÿ ñòðîêå
  12. const char* MyString::GetString() const
  13. {
  14. return m_pStr;
  15. }
  16. //êîíñòðóêòîð êîïèðîâàíèÿ
  17. MyString::MyString(const MyString& Other)
  18. {
  19. m_pStr = new char[strlen(Other.m_pStr) + 1];
  20. strcpy_s(m_pStr, strlen(Other.m_pStr) + 1, Other.m_pStr);
  21. }
  22. //ïåðåìåùàþùèé êîíñòðóêòîð êîïèðîâàíèÿ
  23. MyString::MyString(MyString&& Other)
  24. {
  25. m_pStr = Other.m_pStr; //ïðîñòî çàáèðàåì äàííûå (ñèàìñêèå)
  26. Other.m_pStr = nullptr;//ãîâîðèì, ÷òî ó äðóãîãî íåò (òåïåðü âñå îòëè÷íî)
  27. }
  28. // Îïðåäåëåíèå äåñòðóêòîðà.
  29. MyString::~MyString()
  30. {
  31. //to_do m_pStr = new char [strlen(Str) + 1];
  32. delete[] m_pStr;
  33. m_pStr = nullptr;
  34. }
  35. //ìåòîä, êîòîðûé áóäåò çàìåíÿòü ñòðîêó íà íîâóþ
  36. void MyString::SetNewString(const char* Str)
  37. {
  38. //if (strlen(m_pStr) >= strlen(Str)) {}; íóæíà ëè ïðîâåðêà???
  39. if (strcmp(m_pStr, Str))
  40. {
  41. if (strlen(m_pStr) != strlen(Str))
  42. {
  43. delete[] m_pStr;
  44. m_pStr = new char[strlen(Str) + 1];
  45. };
  46. strcpy_s(m_pStr, strlen(Str) + 1, Str);
  47. }
  48. }
  49. MyString concatenation(const char* Str, ...)
  50. {
  51. size_t size = 1;
  52. for (const char** p = &Str; *(p) != nullptr; p++)
  53. {
  54. size +=strlen(*p);
  55. }
  56. char* tmp_Str = new char [size] {};
  57. for (const char** p = &Str; *(p) != nullptr; p++)
  58. {
  59. tmp_Str = strcat(tmp_Str, *p);
  60. }
  61. MyString tmp (tmp_Str);
  62. delete[]tmp_Str;//áåç óòå÷êè ïàìÿòè
  63. return tmp;//ïåðåìåùàþùèé êîíñòðóêòîð
  64. }