123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- #define _CRT_SECURE_NO_WARNINGS
- #include <string>
- #include "myString.h"
- using namespace std;
- // Îïðåäåëåíèå êîíñòðóêòîðà.
- MyString::MyString(const char* Str)
- {
- m_pStr = new char[strlen(Str) + 1];
- strcpy_s(m_pStr, strlen(Str) + 1, Str);
- }
- //ìåòîä , êîòîðûé îáåñïå÷èò äîñòóï ê õðàíÿùåéñÿ ñòðîêå
- const char* MyString::GetString() const
- {
- return m_pStr;
- }
- //êîíñòðóêòîð êîïèðîâàíèÿ
- MyString::MyString(const MyString& Other)
- {
- m_pStr = new char[strlen(Other.m_pStr) + 1];
- strcpy_s(m_pStr, strlen(Other.m_pStr) + 1, Other.m_pStr);
- }
- //ïåðåìåùàþùèé êîíñòðóêòîð êîïèðîâàíèÿ
- MyString::MyString(MyString&& Other)
- {
- m_pStr = Other.m_pStr; //ïðîñòî çàáèðàåì äàííûå (ñèàìñêèå)
- Other.m_pStr = nullptr;//ãîâîðèì, ÷òî ó äðóãîãî íåò (òåïåðü âñå îòëè÷íî)
- }
- // Îïðåäåëåíèå äåñòðóêòîðà.
- MyString::~MyString()
- {
- //to_do m_pStr = new char [strlen(Str) + 1];
- delete[] m_pStr;
- m_pStr = nullptr;
- }
- //ìåòîä, êîòîðûé áóäåò çàìåíÿòü ñòðîêó íà íîâóþ
- void MyString::SetNewString(const char* Str)
- {
- //if (strlen(m_pStr) >= strlen(Str)) {}; íóæíà ëè ïðîâåðêà???
- if (strcmp(m_pStr, Str))
- {
- if (strlen(m_pStr) != strlen(Str))
- {
- delete[] m_pStr;
- m_pStr = new char[strlen(Str) + 1];
- };
-
- strcpy_s(m_pStr, strlen(Str) + 1, Str);
- }
- }
- MyString concatenation(const char* Str, ...)
- {
- size_t size = 1;
- for (const char** p = &Str; *(p) != nullptr; p++)
- {
- size +=strlen(*p);
- }
-
- char* tmp_Str = new char [size] {};
- for (const char** p = &Str; *(p) != nullptr; p++)
- {
- tmp_Str = strcat(tmp_Str, *p);
- }
-
- MyString tmp (tmp_Str);
- delete[]tmp_Str;//áåç óòå÷êè ïàìÿòè
- return tmp;//ïåðåìåùàþùèé êîíñòðóêòîð
- }
|